在单例中的 PHP CRUD 上的两个实例



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

https://pastebin.com/ZqSCnjqf - 康涅狄格州

https://pastebin.com/301Maf59 - 克鲁德

问题是:当我使用 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 上设置表,就像这样删除:

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

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

我卡在这一点上,我的代码必须在数据库中找到具有产品 ID 的所有图像,通过取消链接文件夹中的文件来删除它们,然后从表中删除产品。

$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 做到这一点?这是正确的工作方法吗?

欢迎任何帮助!

谢谢!

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

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

您使用的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;
}

第一次调用 getInstance 时,它会在静态类中设置实例,并在返回从第一次调用中已设置的实例后的所有后续调用。无论您是否提供不同的连接或表。

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

最新更新