【问题标题】:CakePHP 3 Example Update to SaveMany not Insert to SaveManyCakePHP 3 示例更新 SaveMany 不插入 SaveMany
【发布时间】:2017-05-30 21:22:02
【问题描述】:

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

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

$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);

【问题讨论】:

    标签: cakephp orm cakephp-3.0 updatemodel savemany


    【解决方案1】:

    解决我在 CakePHP 3 中的问题以 saveMany(),

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

    ->patchEntities() 是 saveMany() 的多行“实体”更新

    ;-)

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多