代码之家  ›  专栏  ›  技术社区  ›  kold-kreator

在singleton中有两个关于php crud的实例

  •  1
  • kold-kreator  · 技术社区  · 8 年前

    这是我的CRUD文件和连接文件。

    https://pastebin.com/ZqSCnjqf

    https://pastebin.com/301Maf59 -积垢

    $pdo        = Connection::getInstance();
    $crud       = Crud::getInstance($pdo);
    # ----------------------------------------
    $sql        = "SELECT * FROM images WHERE prod_id = ?";
    $arrayParam = array($prod_id);
    $data_img   = $crud->getSQLGeneric($sql, $arrayParam, true);
    

    $pdo        = Connection::getInstance();
    $crud       = Crud::getInstance($pdo,'images');
    # ----------------------------------------
    $arrayImg   = array('img_id=' => $img_id);
    $return     = $crud->delete($arrayImg);
    

    我不能同时执行这两个语句。假设我需要在同一个代码块中进行插入和删除,它只运行其中的一个。

    $prod_id    = $_GET['prod'];
    # -----
    $pdo        = Connection::getInstance();
    # ----------------------------------------
    $crud       = Crud::getInstance($pdo);
    # -----
    $sql        = "SELECT * FROM images WHERE prod_id = ?";
    $arrayParam = array($prod_id);
    $data_img   = $crud->getSQLGeneric($sql, $arrayParam, true);
    # -----
    foreach($data_img as $img_info)
    {
      unlink('../../img/'.$img_info->img_name);
      $arrayImg = array('img_id=' => $img_info->img_id);
      $return2  = $crud->delete($arrayImg);
    }
    # ----------------------------------------
    $crud       = Crud::getInstance($pdo,'products');
    # -----
    $arrayDel   = array('prod_id=' => $prod_id);
    $return     = $crud->delete($arrayDel);
    # ----------------------------------------
    echo 'Deleted';
    

    欢迎任何帮助!

    2 回复  |  直到 8 年前
        1
  •  1
  •   Yanis-git    8 年前

    我建议你用这个来修改你的单例逻辑。

    class Crud
    {
        private $pdo         = null; # Storing PDO connection
        private $table       = null; # Storing table name
        private static $crud = []; # Static attribute that contains a self instance
    
      # ----------------------------------------
        # Class constructor -> PUBLIC method
        # ----------------------------------------
        public function __construct($connection, $table = 'default')
        {
            if (!empty($connection)) {
                $this->pdo = $connection;
            } else {
                echo 'Conexão inexistente!';
                exit();
            }
    
            if (!empty($table) && $table !== 'default') {
                $this->table =$table;
            }
        }
    
        # ----------------------------------------
        # Static public method that returns a Crud class instance
        # ----------------------------------------
        public static function getInstance($connection, $table = 'default')
        {
            # Verifying if there's a class instance
            if (!isset(self::$crud[$table])) {
                try {
                    self::$crud[$table] = new Crud($connection, $table);
                } catch (Exception $e) {
                    echo 'Error '.$e->getMessage();
                }
            }
            return self::$crud[$table];
        }
    }
    

    $crud 作为数组,并按表名隔离存储每个实例。

            if (!isset(self::$crud[$table])) {
                try {
                    self::$crud[$table] = new Crud($connection, $table);
                } catch (Exception $e) {
                    echo 'Error '.$e->getMessage();
                }
            }
    

    因此,以下代码的工作方式如下:

    Crud::getInstance($pdo); // Create new instance and store it on key 'default'
    Crud::getInstance($pdo); // Just return 'default' instance
    
    Crud::getInstance($pdo,'foo'); // Create new instance and store it on key 'foo'
    Crud::getInstance($pdo,'foo'); // Just return 'foo' instance
    
        2
  •  0
  •   vuryss    8 年前

    Crud

    public static function getInstance($connection, $table=null)
      {
        # Verifying if there's a class instance
       if (!isset(self::$crud))
        {
          try
          {
            self::$crud = new Crud($connection, $table);
          }
          catch (Exception $e)
          {
            echo 'Error '.$e->getMessage();
          }
        }
        return self::$crud;
      }
    

    在这种情况下,您必须为不同的表/连接使用新的CRUD实例。不要使用“getInstance”,而是使用 new Crud(...)