PHP 单例 CRUD 上的两个实例

Two instances on PHP CRUD in singleton

我正在使用在 PHP 中使用单例模式编写的 CRUD class。 这是我的 CRUD 文件和连接文件。

https://pastebin.com/ZqSCnjqf - 连接

https://pastebin.com/301Maf59 - CRUD

问题是:当我使用不需要特定 table 选择的 SELECT 时,我可以有多少选择。像这样:

$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);

但是当我需要删除、插入或更新时,我必须在 CRUD 上设置 table,像这样 delete:

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

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

我被困在这一点上,我的代码必须在数据库中找到所有具有产品 ID 的图像,将它们一并删除,然后取消链接文件夹中的文件,然后从 table 中删除产品.

$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';

有什么方法可以用那个 CRUD 来做到这一点吗?是正确的处理方法吗?

欢迎任何帮助!

谢谢!

您使用的Crud class只允许在其中存储一个实例。之后没有修改。这是从这部分代码完成的:

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;
  }

第一次调用 getInstance 时,它​​会在静态 class 中设置实例,之后的所有后续调用 returns 实例已从第一次调用中设置。不管你给的是不同的连接还是表。

在这种情况下,您将不得不为不同的 tables/connections 使用新的 Crud 实例。不要使用 'getInstance' 而是使用 new Crud(...).

创建一个新实例

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

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 转换为数组,并通过 table 名称隔离存储每个实例。

此处突出显示重要更改:

        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