PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, January 11, 2022

[FIXED] CakePHP updating multiple rows with record ids, same model

 January 11, 2022     cakephp, model     No comments   

Issue

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 ....


Solution

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


Answered By - Nunser
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing