Model::delete expects an idDelete 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-要删除的记录IDinteger|string $id optional null - ID of record to delete布尔$ cascade可选trueboolean $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 errorCalling 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` = 16AND `ProductVouchers`.`client_id` IS NULL;只需禁用 $ cascade :$model->deleteAll( [ 'ProductVouchers.id' => 16, 'ProductVouchers.client_id' => null ], false # <- single delete statement please); 这篇关于CakePHP具有多个条件的删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-02 03:00