8.5.x QueryInterface.php QueryInterface::orConditionGroup()
8.0.x QueryInterface.php QueryInterface::orConditionGroup()
8.1.x QueryInterface.php QueryInterface::orConditionGroup()
8.2.x QueryInterface.php QueryInterface::orConditionGroup()
8.3.x QueryInterface.php QueryInterface::orConditionGroup()
8.4.x QueryInterface.php QueryInterface::orConditionGroup()
8.6.x QueryInterface.php QueryInterface::orConditionGroup()

Creates a new group of conditions ORed together.

For example, consider a map entity with an 'attributes' field containing 'building_type' and 'color' columns. To find all green and red bikesheds:

$query = \Drupal::entityQuery('map');
$group = $query
  ->orConditionGroup()
  ->condition('attributes.color', 'red')
  ->condition('attributes.color', 'green');
$entity_ids = $query
  ->condition('attributes.building_type', 'bikeshed')
  ->condition($group)
  ->execute();

Note that this particular example can be simplified:

$entity_ids = $query
  ->condition('attributes.color', array(
  'red',
  'green',
))
  ->condition('attributes.building_type', 'bikeshed')
  ->execute();

Return value

\Drupal\Core\Entity\Query\ConditionInterface

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

File

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

Class

QueryInterface
Interface for entity queries.

Namespace

Drupal\Core\Entity\Query

Code

public function orConditionGroup();

Comments

UnsettlingTrend’s picture

I'd gone off the "simplified" example above, and it was working. Then it wasn't for some reason. I'm not sure what caused it (it may have been a mysql update) but i would get the error: "SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens"

The solution seemed to be putting the explicit "IN" in the query, making the simplified version:

 $entity_ids = $query
    ->condition('attributes.color', array('red', 'green'), 'IN')
    ->condition('attributes.building_type', 'bikeshed')
    ->execute();
taggartj’s picture

  $s = Drupal::service('entity.query');
    $query = $s->get('some_data');
    $group = $query->orConditionGroup()
     ->condition('id', '4_', 'STARTS_WITH')
     ->condition('id', '5_', 'STARTS_WITH');
    $query->condition($group);
    $entity_ids = $query->execute();
    dump($entity_ids);