Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple conditions for db_update

Tags:

drupal-7

In Drupal 7 simple updates work like this:

$num_updated = db_update('joke')
->fields(array(
'punchline' => 'Take my wife please!',
))
->condition('nid', 3, '>=')
->execute();

But what if I have multiple conditions( say nid >=3 and uid >=2). Writing something like:

$num_updated = db_update('joke')
->fields(array(
'punchline' => 'Take my wife please!',
))
->condition('nid', 3, '>=')
->condition('uid', 2, '>=')
->execute();

does not seem to work. Any ideas?

like image 658
ayush Avatar asked Feb 24 '23 14:02

ayush


1 Answers

What you have written will do the equivalent of:

'...WHERE (NID >= 3 AND UID >= 2)"

If you wanted an OR conditional you need to nest the statements as such (no, it's not very intuitive):

->condition(db_or()->condition('NID'', 3, '>=')->condition('UID', 2, '>='))

There is also db_and() [default for chaining multiple condition methods] and db_xor() that you can use when nesting.

like image 78
mattacular Avatar answered Mar 16 '23 13:03

mattacular