8.3.x QueryInterface.php public QueryInterface::andConditionGroup()
8.0.x QueryInterface.php public QueryInterface::andConditionGroup()
8.1.x QueryInterface.php public QueryInterface::andConditionGroup()
8.2.x QueryInterface.php public QueryInterface::andConditionGroup()
8.4.x QueryInterface.php public QueryInterface::andConditionGroup()

Creates a new group of conditions ANDed together.

For example, consider a drawing entity type with a 'figures' multi-value field containing 'shape' and 'color' columns. To find all drawings containing both a red triangle and a blue circle:

  $query = \Drupal::entityQuery('drawing');
  $group = $query->andConditionGroup()
    ->condition('figures.color', 'red')
    ->condition('figures.shape', 'triangle');
  $query->condition($group);
  $group = $query->andConditionGroup()
    ->condition('figures.color', 'blue')
    ->condition('figures.shape', 'circle');
  $query->condition($group);
  $entity_ids = $query->execute();

Return value

\Drupal\Core\Entity\Query\ConditionInterface

1 method overrides QueryInterface::andConditionGroup()
QueryBase::andConditionGroup in core/lib/Drupal/Core/Entity/Query/QueryBase.php
Creates a new group of conditions ANDed together.

File

core/lib/Drupal/Core/Entity/Query/QueryInterface.php, line 220

Class

QueryInterface
Interface for entity queries.

Namespace

Drupal\Core\Entity\Query

Code

public function andConditionGroup();

Comments

seemas’s picture

Took me some time to configure how to construct different types of filtering tags. I think this will be better example of WORKING code:

switch ($type) {
        case 'or' : $query->condition('field_tags', $tags, 'IN');
          break;
        case 'and':
          foreach ($tags as $tag) {
            $query->condition($query->andConditionGroup()
                    ->condition('field_tags', $tag));
          }
          break;
      }

And this is an example how my logic was trying to build and this code does NOT WORK:

switch ($type) {
        case 'or' : $query->condition('field_tags', $tags, 'IN');
          break;
        case 'and':
          $and = $query->andConditionGroup();
          foreach ($tags as $tag) {
            $and->condition('field_tags', $tag);
          }
          $query->condition($and);
          break;
      }