我想一次更新特定模型的多行,所以我有以下代码和数据结构。每当我尝试在每次插入记录而不是更新时更新记录。
在我的 Controller 中
function products($cat_id = null){
$products = $this->Products->find()->where(['category_id' => $cat_id]);
$productPatch = $this->Products->patchEntities($products, $this->request->data);
$this->Products->saveMany($productPatch); //Always creates new record
}
这是不同数组中的数据,
In $products
[0] => Array
(
[id] => 13 //product_id
[category_id] => 17
[slug] => onScreen
[status_id] => 1
)
[1] => Array
(
[id] => 14
[category_id] => 17
[slug] => pdf
[status_id] => 1
)
In $this->request->data
[0] => Array
(
[id] => 13 //product_id
[category_id] => 17
[slug] => onScreen
[status_id] => 2 //Data changes here
)
[1] => Array
(
[id] => 14
[category_id] => 17
[slug] => pdf
[status_id] => 2 //Data changes here
)
最佳答案
您可以使用 updateAll() 方法批量更新数据:
$this->modelClass->updateAll(
['published' => true], // fields
['published' => false]); // conditions
关于cakephp - 如何更新模型的多行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46302192/