Reputation: 821
I want to update multiple rows of a specific model at once, so I have bellow code and data structure. Whenever I tried to update the record every time record gets inserted instead of update.
in my 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
}
Here is the data in the different array,
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
)
Upvotes: 3
Views: 674
Reputation: 405
You can use updateAll() method to update data in Bulk :
$this->modelClass->updateAll(
['published' => true], // fields
['published' => false]); // conditions
Upvotes: 1