CakePHP具有多个条件的删除 [英] CakePHP Delete with Multiple Conditions

查看:54
本文介绍了CakePHP具有多个条件的删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is driving me mad. Why does Cake need to make simple things overly complex..

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

I am hoping to get Cake to generate SQL that looks like this..

我希望运行的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'
 ]);

日志什么都没有显示,这甚至是错误的!

Log shows nothing which is even more wrong!

delete()方法是错误的,因为它仅接受主键作为整数.因此,使用 deleteAll()

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

这也不会在日志中产生任何结果或任何错误.正在尝试:

This also produces nothing in the log or any error. Trying:

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

这会产生...

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

既错又怪(括号的意思是什么?)

Which is both wrong and weird (whats the point of the brackets??)

推荐答案

Model :: delete需要一个ID

删除是一种通过主键值删除一行的方法. Model :: delete 的方法签名不符合条件:

Model::delete expects an id

Delete is a method for deleting one row, by primary key value. The method signature for Model::delete does not expect conditions:

删除()公共

删除给定ID的记录.如果没有给出ID,则使用当前ID.成功返回true.

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

参数

整数|字符串$ id可选null-要删除的记录ID

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

布尔$ cascade可选true

boolean $cascade optional true

被称为传递数组的行为是,它不能以一种或另一种方式起作用(在这种情况下,conditions数组的数组值被理解为id-并用于查找 a 记录删除-最多删除一行).

The behavior when called passing an array is that it won't work, one way or another (in the case here, the array values of the conditions array are understood to be ids - and used to find a record to delete - it would delete at most one row).

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

deleteAll is a method for deleting rows by conditions:

/**
 * 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) {

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

The correct call for the logic in the question would be:

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

这也不会在日志中产生任何结果或任何错误

使用 $ cascade true(默认)调用deleteAll表示:

This also produces nothing in the log or any error

Calling deleteAll with $cascade true (the default) means:

  • 查找所有符合条件的记录
  • 一个一个地删除它们

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

If no records are found matching the conditions, there will be no delete statement.

此条件片段:

'ProductVouchers.client_id'=>'NULL'

将生成此sql:

WHERE ProductVouchers.client_id = 'NULL'

即它将返回其中client_id为字符串"NULL"的行-由于没有 no 行,其中client_id设置为字符串"NULL",因此没有delete语句.

I.e. it will return rows where client_id is the string "NULL" - hence there is no delete statement because there are no rows with a client_id set to the string "NULL".

实际上,如果存在一条记录,该记录的主键和client_id设置为null(实际上,在该记录之前将有一个select语句,用于通过id和client_id值进行查找),则该语句应为.括号没有任何功能相关,这两个语句是等效的:

Actually that statement is expected if there is a record with that primary key and client_id set to null (there will be a select statement immediately before it to find by id and client_id value). The parentheses are of no functional relevance, these two statements are equivalent:

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

如何按条件删除?

要生成与此sql等效的内容:

How to delete by conditions?

To generate the equivalent of this 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
);

这篇关于CakePHP具有多个条件的删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆