0

In Cakephp 2's model updateAll, we were using arrays as conditions. But this doesn't work :

$data=array(12, 22, 44);
TableRegistry::get('Foobar')->updateAll(
   array("checked" => 1),
   array("user_id" => $data) 
 );

Is this not possible in Cakephp 3's updateAll ?

This works in cakephp 3:

$data=12;
TableRegistry::get('Foobar')->updateAll(
   array("checked" => 1),
   array("user_id" => $data) 
 );

Regarding to API docs, $conditions defined like this:

updateAll( array $fields , mixed $conditions )

array $fields
A hash of field => new value.

mixed $conditions
Conditions to be used, accepts anything Query::where() can take.

Edit:
In case of cake works well and I'm making a mistake. How can I understand the cause ? Because when condition is an integer, code works well.

Edit2: This also works well:

 $data=array(12, 22, 44);
 ConnectionManager::get('default')->query('UPDATE foobars SET checked=1 
     WHERE user_id IN ('.implode(",",$data).')')->execute();
4
  • You are missing the second argument in the function call Commented Jul 16, 2015 at 20:39
  • Sorry I couldn't understand. Do you mean updateAll function ? Commented Jul 16, 2015 at 20:44
  • stackoverflow.com/questions/26887511/where-in-clause I hope you are aware that your raw query is prone to SQL injections in case $data will be user input! Commented Jul 17, 2015 at 14:04
  • Thank you ndm. Yes, thats why I need to create a cakephp type query, rather than a raw query. I suppose the problem is missing of IN keyword. I couldnt see it in orm migration notes. book.cakephp.org/3.0/en/appendices/orm-migration.html Also I would be happy to accept your reply as an answer to this question. Commented Jul 17, 2015 at 14:22

2 Answers 2

1

It does work, as explained in the docs:

http://book.cakephp.org/3.0/en/orm/saving-data.html#bulk-updates

Sign up to request clarification or add additional context in comments.

3 Comments

Is there a test case that checks for updateAll, with array conditions that has one key and multiple values ? github.com/cakephp/cakephp/…
It seems like there is not... What does you intuition says about it? How would you use multiple set and multiple condiitions?
I updated question, you can see from edit2 that, Connection->query() works well for this situation. I suppose I make a mistake using updateAll method. But not yet found the cause.
1

A little late to the discussion, but since I had a similar problem, using something like id in array condition, this is how is done now on Cakephp 3.

//using the same example
$data=array(12, 22, 44);

TableRegistry::get('Foobar')->updateAll(
   ["checked" => 1],
   ["user_id IN " => $data] //mind the IN on the condition
);

Here is another way from the docs, updated for the example

$articles = TableRegistry::get('Articles');
$data=array(12, 22, 44);

$query = $articles->query();
$query->update()
    ->set(['checked' => true])
    ->where(['user_id IN' => $data])
    ->execute();

Hope it helps someone else.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.