class ConditionAggregate

Defines the aggregate condition for sql based storage.

Hierarchy

Expanded class hierarchy of ConditionAggregate

1 file declares its use of ConditionAggregate
EntityQueryAggregateTest.php in drupal/core/modules/system/lib/Drupal/system/Tests/Entity/EntityQueryAggregateTest.php
Contains \Drupal\system\Tests\Entity\EntityQueryAggregateTest.

File

drupal/core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Entity/ConditionAggregate.php, line 17
Contains \Drupal\field_sql_storage\Query\ConditionAggregate.

Namespace

Drupal\field_sql_storage\Entity
View source
class ConditionAggregate extends ConditionAggregateBase {

  /**
   * Implements \Drupal\Core\Entity\Query\ConditionInterface::compile().
   */
  public function compile($conditionContainer) {

    // If this is not the top level condition group then the sql query is
    // added to the $conditionContainer object by this function itself. The
    // SQL query object is only necessary to pass to Query::addField() so it
    // can join tables as necessary. On the other hand, conditions need to be
    // added to the $conditionContainer object to keep grouping.
    $sql_query = $conditionContainer instanceof SelectInterface ? $conditionContainer : $conditionContainer->sqlQuery;
    $tables = new Tables($sql_query);
    foreach ($this->conditions as $condition) {
      if ($condition['field'] instanceof ConditionAggregateInterface) {
        $sql_condition = new Condition($condition['field']
          ->getConjunction());

        // Add the SQL query to the object before calling this method again.
        $sql_condition->sqlQuery = $sql_query;
        $condition['field']
          ->compile($sql_condition);
        $sql_query
          ->condition($sql_condition);
      }
      else {
        $type = strtoupper($this->conjunction) == 'OR' || $condition['operator'] == 'IS NULL' ? 'LEFT' : 'INNER';
        $this
          ->translateCondition($condition);
        $field = $tables
          ->addField($condition['field'], $type, $condition['langcode']);
        $function = $condition['function'];
        $placeholder = ':db_placeholder_' . $conditionContainer
          ->nextPlaceholder();
        $conditionContainer
          ->having("{$function}({$field}) {$condition['operator']} {$placeholder}", array(
          $placeholder => $condition['value'],
        ));
      }
    }
  }

  /**
   * Implements \Drupal\Core\Entity\Query\ConditionInterface::exists().
   */
  public function exists($field, $function, $langcode = NULL) {
    return $this
      ->condition($field, $function, NULL, 'IS NOT NULL', $langcode);
  }

  /**
   * Implements \Drupal\Core\Entity\Query\ConditionInterface::notExists().
   */
  public function notExists($field, $function, $langcode = NULL) {
    return $this
      ->condition($field, $function, NULL, 'IS NULL', $langcode);
  }

  /**
   * Translates the string operators to SQL equivalents.
   *
   * @param array $condition
   *   An associative array containing the following keys:
   *     - value: The value to filter by
   *     - operator: The operator to use for comparison, for example "=".
   */
  protected function translateCondition(&$condition) {
    switch ($condition['operator']) {
      case 'STARTS_WITH':
        $condition['value'] .= '%';
        $condition['operator'] = 'LIKE';
        break;
      case 'CONTAINS':
        $condition['value'] = '%' . $condition['value'] . '%';
        $condition['operator'] = 'LIKE';
        break;
      case 'ENDS_WITH':
        $condition['value'] = '%' . $condition['value'];
        $condition['operator'] = 'LIKE';
        break;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConditionAggregate::compile public function Implements \Drupal\Core\Entity\Query\ConditionInterface::compile(). Overrides ConditionAggregateInterface::compile
ConditionAggregate::exists public function Implements \Drupal\Core\Entity\Query\ConditionInterface::exists(). Overrides ConditionAggregateInterface::exists
ConditionAggregate::notExists public function Implements \Drupal\Core\Entity\Query\ConditionInterface::notExists(). Overrides ConditionAggregateInterface::notExists
ConditionAggregate::translateCondition protected function Translates the string operators to SQL equivalents.
ConditionAggregateBase::condition public function Implements \Drupal\Core\Entity\Query\ConditionAggregateInterface::condition(). Overrides ConditionAggregateInterface::condition
ConditionFundamentals::$conditions protected property Array of conditions.
ConditionFundamentals::$conjunction protected property The conjunction of this condition group. The value is one of the following:
ConditionFundamentals::conditions public function Implements \Drupal\Core\Entity\Query\ConditionInterface::conditions().
ConditionFundamentals::count public function Implements \Countable::count().
ConditionFundamentals::getConjunction public function Implements \Drupal\Core\Entity\Query\ConditionInterface::getConjunction().
ConditionFundamentals::__clone public function Implements the magic __clone function.
ConditionFundamentals::__construct public function Constructs a Condition object.