这让我发疯。为什么 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)
哪个既错误又奇怪(括号的意义是什么??)
最佳答案
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 要删除的记录 - 它最多会删除一行)。
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/
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html
我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2
我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的
我正在尝试修改当前依赖于定义为activeresource的gem:s.add_dependency"activeresource","~>3.0"为了让gem与Rails4一起工作,我需要扩展依赖关系以与activeresource的版本3或4一起工作。我不想简单地添加以下内容,因为它可能会在以后引起问题:s.add_dependency"activeresource",">=3.0"有没有办法指定可接受版本的列表?~>3.0还是~>4.0? 最佳答案 根据thedocumentation,如果你想要3到4之间的所有版本,你可以这
我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin