ormcakephp-3.0updatemodelcakephp

CakePHP 3 Example Update to SaveMany not Insert to SaveMany


I have trouble updating multiple records. It does not work for me and in the CakePHP documentation there is nothing about Update there is only to give of multiple high register, but nothing update with existing fields.

My Example Error, Not 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);

Solution

  • Solutin my problema in CakePHP 3 to saveMany(),

    ->patchEntity() is one row "Entity" update, for save()

    ->patchEntities() is Multiple rows "Entities" Update for saveMany()

    ;-)

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