Mittul At TechnoBrave Mittul At TechnoBrave - 1 year ago 126
MySQL Question

Use IN clause in updateAll() method in CakePHP 3.x

I am using Cakephp 3.x and i want to update my single field for multiple ids. Something like this..

UPDATE mytable SET `status` = '1' WHERE ID IN (1,2,3)

Currently i am using query to perform this action using cakpehp

->set(['leaveregister_status' => $this->request->data('status')])
->where(['leaveregister_id IN ('.$this->request->data('final_ids_string').')'])

Well this does the trick for me but i want this to be performed using cakephp 3.xs' ORM method .. so i am trying to use this instead

$table->updateAll(['field' => $newValue], ['id' => $entityId]);

But this code is for single id only which i do not want.. i also do not want to use
loop to perform the same action. Instead i want an ID to be passed via array or comma seperated in any case and want to perform the same action.

Is there any way i can perform the same thing using ORM method using cakephp 3.x


Answer Source

usinga updateAll or a query() objects to do a bulk update is the same thing as you can read in the manual at the end of this paragraph

so you can do

    ->set(['leaveregister_status' => $this->request->data('status')])
    ->where(['leaveregister_id IN' => [1,2,3])


    ['leaveregister_status' => $this->request->data('status')]
    ['leaveregister_id IN' => [1,2,3]);

remember then when usin IN clause you have to pass an array. Read this part of the manual on how to create IN clause

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download