About UpdateAll

Asked 2 years ago, Updated 2 years ago, 56 views

save() and updateAll() are different, the modified column does not update automatically, and you must sanitize the values you want to update yourself.

How do you think this is the implementation?

cakephp

2022-09-30 19:38

1 Answers

If you think you have to sanitize the value, it's a disadvantage, but you can also specify an expression for SQL instead of a disadvantage.This is useful for updating multiple records at once.

For example, I would like to increase the amount of money owned by users under the age of 20 by 100.

If you want to use save, you will get the target record with find('all',...) and update it one by one.This is inefficient.I could write SQL like this.

UPDATE users SET money=money+1WHEREage<20

The same thing can be done on CakePHP with updateAll.

$this->User->updateAll(
    array('money'=>'money+100'),
    array('age<'=>'20')
);

If updateAll is an automatically sanitized specification, you cannot write the formula money+100 and you end up with the string "money+100".

I can't think of the obvious reason why modified is not updated, but if you think of it as something like an update query, you may not want to do anything unnecessary.


2022-09-30 19:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.