Generally in Drupal query builder, we can chain condition() method to add AND conditions to the query we are building.
Which will create a query something like 
WHERE Condition1 AND Condition2 AND Condition3
by following code:
<?php $query->condition(Condition1)->condition(Condition2)->condition(Condition3); ?>
 
Now If we want OR,
<?php $query->condition(db_or()->condition(Condition1)->condition(Condition2)->condition(Condition3)); ?>
 
If we want to nest OR inside AND,
something like 
WHERE (Condition1 OR Condition2) AND (Condition3 OR Condition4)

You can do:
<?php 
$query->condition(db_or()->condition(Condition1)->condition(Condition2));
$query->condition(db_or()->condition(Condition3)->condition(Condition4));
?>
 
If we want to nest AND inside OR,
something like 
WHERE (Condition1 AND Condition2) OR (Condition3 AND Condition4)
You can do:
<?php 
$query->condition(db_or()->condition(db_and()->condition(Condition1)->condition(Condition2))
      ->condition(db_and()->condition(Condition3)->condition(Condition4)));
?>

 

Comments

comments

Uncategorized

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.