Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP updating multiple rows with record ids, same model

Tags:

model

cakephp

I want to update a model. I have a query like this

UPDATE mymodels SET `myfield` = 100 WHERE `id`=12 OR `id`=13 OR `id`=14
// also the same:
UPDATE mymodels SET `myfield` = 100 WHERE `id` IN (12,13,14)

And I tried this:

$this->MyModel->updateAll(
    // fields
    array('MyModel.myfield' => 100),
    // conditions
    array('MyModel.id' => 12)
);

But I need to update 20 different records.
Record ids are like this 12, 13, 14 ....

like image 299
trante Avatar asked Jan 09 '23 23:01

trante


1 Answers

If the records you want to update are sequential, then try adding two conditions like this

$this->MyModel->updateAll(
    array('MyModel.myfield' => 100),
    // conditions
    array('MyModel.id >=' => 12, 'MyModel.id <=' => 20)
);

If there's no relation between indexes of ids, try using an array

$this->MyModel->updateAll(
    array('MyModel.myfield' => 100),
    // conditions
    array('MyModel.id' => array(12,13,14...))
);
like image 109
Nunser Avatar answered Jan 12 '23 11:01

Nunser