function Condition::compile

Same name in this branch
  1. main core/lib/Drupal/Core/Config/Entity/Query/Condition.php \Drupal\Core\Config\Entity\Query\Condition::compile()
  2. main core/lib/Drupal/Core/Database/Query/Condition.php \Drupal\Core\Database\Query\Condition::compile()
  3. main core/lib/Drupal/Core/Entity/Query/Null/Condition.php \Drupal\Core\Entity\Query\Null\Condition::compile()
Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Config/Entity/Query/Condition.php \Drupal\Core\Config\Entity\Query\Condition::compile()
  2. 11.x core/lib/Drupal/Core/Database/Query/Condition.php \Drupal\Core\Database\Query\Condition::compile()
  3. 11.x core/lib/Drupal/Core/Entity/Query/Sql/Condition.php \Drupal\Core\Entity\Query\Sql\Condition::compile()
  4. 11.x core/lib/Drupal/Core/Entity/Query/Null/Condition.php \Drupal\Core\Entity\Query\Null\Condition::compile()
  5. 10 core/lib/Drupal/Core/Config/Entity/Query/Condition.php \Drupal\Core\Config\Entity\Query\Condition::compile()
  6. 10 core/lib/Drupal/Core/Database/Query/Condition.php \Drupal\Core\Database\Query\Condition::compile()
  7. 10 core/lib/Drupal/Core/Entity/Query/Sql/Condition.php \Drupal\Core\Entity\Query\Sql\Condition::compile()
  8. 10 core/lib/Drupal/Core/Entity/Query/Null/Condition.php \Drupal\Core\Entity\Query\Null\Condition::compile()
  9. 9 core/lib/Drupal/Core/Config/Entity/Query/Condition.php \Drupal\Core\Config\Entity\Query\Condition::compile()
  10. 9 core/lib/Drupal/Core/Database/Query/Condition.php \Drupal\Core\Database\Query\Condition::compile()
  11. 9 core/lib/Drupal/Core/Entity/Query/Sql/Condition.php \Drupal\Core\Entity\Query\Sql\Condition::compile()
  12. 9 core/lib/Drupal/Core/Entity/Query/Null/Condition.php \Drupal\Core\Entity\Query\Null\Condition::compile()
  13. 8.9.x core/lib/Drupal/Core/Config/Entity/Query/Condition.php \Drupal\Core\Config\Entity\Query\Condition::compile()
  14. 8.9.x core/lib/Drupal/Core/Database/Query/Condition.php \Drupal\Core\Database\Query\Condition::compile()
  15. 8.9.x core/lib/Drupal/Core/Entity/Query/Sql/Condition.php \Drupal\Core\Entity\Query\Sql\Condition::compile()
  16. 8.9.x core/lib/Drupal/Core/Entity/Query/Null/Condition.php \Drupal\Core\Entity\Query\Null\Condition::compile()

Compiles this conditional clause.

Parameters

\Drupal\Core\Entity\Query\QueryInterface $query: The query object this conditional clause belongs to.

Overrides ConditionInterface::compile

File

core/lib/Drupal/Core/Entity/Query/Sql/Condition.php, line 46

Class

Condition
Implements entity query conditions for SQL databases.

Namespace

Drupal\Core\Entity\Query\Sql

Code

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 : $this->sqlQuery;
  // Pass the sql_query in and suppress deprecation handling, so that we can
  // manually handle the deprecation for each condition.
  $tables = $this->query
    ->doGetTables($sql_query);
  foreach ($this->conditions as $key => $condition) {
    if ($condition['field'] instanceof ConditionInterface) {
      $sql_condition = $sql_query->getConnection()
        ->condition($condition['field']->getConjunction());
      // Deprecate using getQuery()->condition() passed into another
      // getQuery() as the objects mismatch. The doGetTables($sql_query)
      // will prevent crashing, but joins will be duplicated.
      if ($condition['field']->getQuery() !== $this->getQuery()) {
        @trigger_error('Passing a Condition to \\Drupal\\Core\\Entity\\Query\\Sql\\Query::condition() that was generated by a different query is deprecated in drupal:11.4.0 and removed in drupal:13.0.0. See https://www.drupal.org/node/3585318', E_USER_DEPRECATED);
      }
      // Add the SQL query to the object before calling this method again.
      $condition['field']->sqlQuery = $sql_query;
      // When using an AND condition group, join tables again for fields
      // inside, but once only per field, so that other AND condition
      // groups can do the same. This ensures that for cardinality > 1
      // fields, the correct number of joins are made to allow us to
      // use condition groups to fetch where a entity has X AND Y.
      // If we did not join the table again, the values only exist once
      // and the AND fails to match even though an entity has both X and Y.
      $condition['field']->tableIndexPrefix = $this->conjunction === 'AND' ? $this->tableIndexPrefix . '.' . $key : $this->tableIndexPrefix;
      $condition['field']->nestedInsideOrCondition = $this->nestedInsideOrCondition || strtoupper($this->conjunction) === 'OR';
      $condition['field']->compile($sql_condition);
      $conditionContainer->condition($sql_condition);
    }
    else {
      $type = $this->nestedInsideOrCondition || strtoupper($this->conjunction) === 'OR' || $condition['operator'] === 'IS NULL' ? 'LEFT' : 'INNER';
      $field = $tables->addField($condition['field'], $this->tableIndexPrefix ? "{$type}:{$this->tableIndexPrefix}" : $type, $condition['langcode']);
      // If the field is trying to query on %delta for a single value field
      // then the only supported delta is 0. No other value than 0 makes
      // sense. \Drupal\Core\Entity\Query\Sql\Tables::addField() returns 0 as
      // the field name for single value fields when querying on their %delta.
      if ($field === 0) {
        if ($condition['value'] != 0) {
          $conditionContainer->alwaysFalse();
        }
        continue;
      }
      $condition['real_field'] = $field;
      if (is_array($condition['value'])) {
        $condition['value'] = array_values($condition['value']);
      }
      static::translateCondition($condition, $sql_query, $tables->isFieldCaseSensitive($condition['field']));
      // Add the translated conditions back to the condition container.
      if (isset($condition['where']) && isset($condition['where_args'])) {
        $conditionContainer->where($condition['where'], $condition['where_args']);
      }
      else {
        $conditionContainer->condition($field, $condition['value'], $condition['operator']);
      }
    }
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.