繁体   English   中英

Cakephp 3表更新全部使用数组

[英]Cakephp 3 table updateAll using array

Cakephp 2的模型updateAll中,我们使用数组作为条件。 但这不起作用:

$data=array(12, 22, 44);
TableRegistry::get('Foobar')->updateAll(
   array("checked" => 1),
   array("user_id" => $data) 
 );

在Cakephp 3的updateAll中这不可能吗?

这适用于cakephp 3:

$data=12;
TableRegistry::get('Foobar')->updateAll(
   array("checked" => 1),
   array("user_id" => $data) 
 );

关于API docs$conditions定义如下:

updateAll( array $fields , mixed $conditions )

array $fields
A hash of field => new value.

mixed $conditions
Conditions to be used, accepts anything Query::where() can take.

编辑:
万一蛋糕很好用,我错了。 我如何理解原因? 因为当condition为整数时,代码运行良好。

Edit2:这也很好:

 $data=array(12, 22, 44);
 ConnectionManager::get('default')->query('UPDATE foobars SET checked=1 
     WHERE user_id IN ('.implode(",",$data).')')->execute();

如文档中所述,它确实可以工作:

http://book.cakephp.org/3.0/en/orm/saving-data.html#bulk-updates

讨论有点晚了,但是由于我遇到了类似的问题,因此在数组条件中使用了类似id的东西,现在在Cakephp 3上就是这样做的。

//using the same example
$data=array(12, 22, 44);

TableRegistry::get('Foobar')->updateAll(
   ["checked" => 1],
   ["user_id IN " => $data] //mind the IN on the condition
);

这是docs的另一种方式,针对示例进行了更新

$articles = TableRegistry::get('Articles');
$data=array(12, 22, 44);

$query = $articles->query();
$query->update()
    ->set(['checked' => true])
    ->where(['user_id IN' => $data])
    ->execute();

希望它可以帮助别人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM