cakephp - 如何更新模型的多行?

标签 cakephp cakephp-3.0 cakephp-3.2

我想一次更新特定模型的多行,所以我有以下代码和数据结构。每当我尝试在每次插入记录而不是更新时更新记录。

在我的 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/

相关文章:

php - CakePHP 多次和条件下价格不起作用

cakephp - 何时在 CakePHP 中进行清理

cakephp 3没有时间显示日期

mysql - CakePHP 3 引用我的 find() 调用的 'fields' 配置中定义的函数名称

cakephp - 如何在 cakephp 3 中运行所有插件的所有迁移?

jquery - 下拉框在谷歌浏览器中不起作用

mysql - 如何在cakephp中编写Mysql DATE_FORMAT?

unit-testing - CakePHP 单元测试中电子邮件中的完整 URL

cakephp - 在 cakephp 3 中用两个外键链接同一个表

php - Redis 作为 CakePHP 3 中的 NoSQL 键/值数据库