php - CakePHP 删除多个条件

标签 php cakephp cakephp-3.0

这让我发疯。为什么 Cake 需要将简单的事情变得过于复杂..

我希望让 Cake 生成看起来像这样的 SQL..

我希望运行的 SQL 是

DELETE `ProductVouchers` 
FROM `product_vouchers` AS `ProductVouchers`   
WHERE `ProductVouchers`.`id` = 16 
AND `ProductVouchers`.`client_id` IS NULL; 

我正在像这样使用delete()

$this->ProductVouchers->delete([
    'ProductVouchers.id'=>$id,
    'ProductVouchers.client_id'=>"NULL"
]);

日志显示

DELETE `ProductVouchers` 
FROM `product_vouchers` AS `ProductVouchers`   
WHERE `ProductVouchers`.`id` IN (16, 'NULL')

尝试

$this->ProductVouchers->delete([
   'ProductVouchers.id'=>$id,
   'ProductVouchers.client_id'=>NULL
]);

日志显示

DELETE `ProductVouchers` 
FROM `product_vouchers` AS `ProductVouchers`   
WHERE `ProductVouchers`.`id` IN (16, NULL) 

尝试:

 $this->ProductVouchers->delete([
    'ProductVouchers.id'=>$id,
    'ProductVouchers.client_id IS NULL'
 ]);

日志没有显示,更错!

编辑:

正如下面的答案所指出的,delete() 方法是不正确的,因为它只接受作为整数的主键。所以使用 deleteAll()

 $this->ProductVouchers->deleteAll([       
   'ProductVouchers.id'=>$id,
   'ProductVouchers.client_id'=>'NULL'
 ]);

这也不会在日志中产生任何内容或任何错误。尝试:

 $this->ProductVouchers->deleteAll([
   'ProductVouchers.id'=>$id,
   'ProductVouchers.client_id'=>NULL
 ]);

这会产生...

 DELETE `ProductVouchers` 
 FROM `product_vouchers` AS `ProductVouchers`   
 WHERE `ProductVouchers`.`id` = (17)

哪个既错误又奇怪(括号的意义是什么??)

最佳答案

Model::delete 需要一个 id

Delete 是一种通过主键值删除一行的方法。 Model::delete 的方法签名不期望条件:

delete( ) public

Removes record for given ID. If no ID is given, the current ID is used. Returns true on success.

Parameters

integer|string $id optional null - ID of record to delete

boolean $cascade optional true

调用传递数组时的行为是它不会以任何方式工作(在这种情况下,条件数组的数组值被理解为 ids - 并用于查找 a 要删除的记录 - 它最多会删除一行)。

Model::deleteAll 预期条件

deleteAll是一种按条件删除行的方法:

/**
 * Deletes multiple model records based on a set of conditions.
 *
 * @param mixed $conditions Conditions to match
 * @param bool $cascade Set to true to delete records that depend on this record
 * @param bool $callbacks Run callbacks
 * @return bool True on success, false on failure
 * @link http://book.cakephp.org/2.0/en/models/deleting-data.html#deleteall
 */
    public function deleteAll($conditions, $cascade = true, $callbacks = false) {

问题中逻辑的正确调用是:

$model->deleteAll(
    [
        'ProductVouchers.id' => 16, 
        'ProductVouchers.client_id' => null
    ],
    $cascade,
    $callbacks
);

这也不会在日志中产生任何内容或产生任何错误

使用 $cascade true(默认值)调用 deleteAll 意味着:

  • 查找符合条件的所有记录
  • 一一删除

如果没有找到符合条件的记录,则不会有删除语句。

这个条件片段:

'ProductVouchers.client_id'=>'NULL'

将生成此 sql:

WHERE ProductVouchers.client_id = 'NULL'

即它将返回 client_id 为字符串“NULL”的行 - 因此没有 delete 语句,因为没有 client_id 设置为字符串“NULL”的行。

哪个既错误又奇怪(括号的意义是什么??)

实际上,如果有一条主键和 client_id 设置为 null 的记录,则该语句是预期的(在它之前将有一个 select 语句,以通过 id 和 client_id 值查找)。括号与功能无关,这两个语句是等价的:

DELETE FROM `product_vouchers` WHERE `ProductVouchers`.`id` = (17)
DELETE FROM `product_vouchers` WHERE `ProductVouchers`.`id` = 17

如何按条件删除?

要生成此 sql 的等价物:

DELETE FROM `product_vouchers` AS `ProductVouchers`   
WHERE `ProductVouchers`.`id` = 16 
AND `ProductVouchers`.`client_id` IS NULL; 

简单地禁用$cascade:

$model->deleteAll(
    [
        'ProductVouchers.id' => 16, 
        'ProductVouchers.client_id' => null
    ],
    false # <- single delete statement please
);

关于php - CakePHP 删除多个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29145540/

相关文章:

php - DIV 向下一行

PHP多维数组按值搜索

PHP 错误和 JavaScript 验证

mysql - 如何在cakephp中查找最新的记录?

cakephp - 如何从 cakephp 文件夹外部包含一个类

php - CakePHP 图标

php - Mysql数据库管理器-CMS

php - 如何在cakephp 3.6中从一个表中选择所有记录并从另一表中选择一些记录

php - 多个相同的实体关系

Cakephp 3如何在表上添加beforeFind