7

I would like to convert the following UpdateQuery statement to work with multiple conditions by using OR instead of the default AND condition:

  db_update('system')
    ->fields(array('status' => 1))
    ->condition('type', 'theme')
    ->condition('name', 'seven') // OR
    ->condition('name', 'rubik')
    ->execute();

It's possible?

8

Drupal 7

Yes, it is possible by using db_or() DatabaseCondition as example below:

  db_update('system')
    ->fields(array('status' => 1))
    ->condition('type', 'theme')
    ->condition(db_or()
      ->condition('name', 'seven')
      ->condition('name', 'rubik')
    )
    ->execute();

Similar for the db_select, see: "OR" condition in db_select() and Nested OR and AND condition with db_select

See also: Multiple conditions for db_update at StackOverflow


Drupal 8

You can still use db_or() as in Drupal 7, but it's deprecated as of Drupal 8.0.x and will be removed in Drupal 9.0.0.

Create a \Drupal\Core\Database\Query\Condition object, specifying an OR conjunction: new Condition('OR'); db_or()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.