CakePHP 3 SaveMany的示例更新未插入SaveMany

时间:2017-01-16 04:45:33

标签: cakephp orm cakephp-3.0 updatemodel savemany

我无法更新多条记录。它对我不起作用,在CakePHP文档中没有任何关于Update的内容,只提供多个高位寄存器,但没有更新现有字段。

我的示例错误,不更新; - (

$ids = [101,123];

$data_update = [
  [
   'id'=>101,
   'licensees_id'=>'10',
  ],
  [
   'id'=>123,
   'licensees_id'=>'10',
  ],
];

// Get all users
    $usersEntity = $this->Users->find('all',[
           'fields'=>[
               'id',
               'licensees_id',
           ],
            'conditions'=>[
                'id IN'=>$ids
            ]
        ]);
        $usersEntity = $this->Users->patchEntity($usersEntity, $data_update); //<= Error parant
//        exit(debug($usersEntity));
        $result = $this->Users->saveMany($usersEntity);

1 个答案:

答案 0 :(得分:1)

在CakePHP 3中解决我的问题以保存许多()

- &gt; patchEntity()是一行“实体”更新,用于save()

- &gt; patchEntities()是多行“实体”更新为saveMany()

- )

// Change Line
$usersEntity = $this->Users->patchEntity($usersEntity, $data_update);
// For
$usersEntity = $this->Users->patchEntities($usersEntity, $users_data);