Several conditions for db_update

In Drupal, 7 simple updates work as follows:

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

But what if I have several 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(); 

doesn't seem to work. Any ideas?

+4
source share
1 answer

What you wrote will make the equivalent:

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

If you need a conditional conditional signal, you need to enclose the instructions as such (no, this is not very intuitive):

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

There is also db_and () [the default for combining multiple condition methods] and db_xor (), which you can use when nesting.

+8
source

Source: https://habr.com/ru/post/1346684/


All Articles