class Condition

Same name in this branch
  1. 9 core/tests/fixtures/database_drivers/module/corefake/src/Driver/Database/corefakeWithAllCustomClasses/Condition.php \Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses\Condition
  2. 9 core/lib/Drupal/Core/Config/Entity/Query/Condition.php \Drupal\Core\Config\Entity\Query\Condition
  3. 9 core/lib/Drupal/Core/Database/Query/Condition.php \Drupal\Core\Database\Query\Condition
  4. 9 core/lib/Drupal/Core/Entity/Query/Sql/pgsql/Condition.php \Drupal\Core\Entity\Query\Sql\pgsql\Condition
  5. 9 core/lib/Drupal/Core/Entity/Query/Null/Condition.php \Drupal\Core\Entity\Query\Null\Condition
  6. 9 core/lib/Drupal/Core/Entity/KeyValueStore/Query/Condition.php \Drupal\Core\Entity\KeyValueStore\Query\Condition
  7. 9 core/lib/Drupal/Core/Condition/Annotation/Condition.php \Drupal\Core\Condition\Annotation\Condition
Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Config/Entity/Query/Condition.php \Drupal\Core\Config\Entity\Query\Condition
  2. 8.9.x core/lib/Drupal/Core/Database/Query/Condition.php \Drupal\Core\Database\Query\Condition
  3. 8.9.x core/lib/Drupal/Core/Entity/Query/Sql/Condition.php \Drupal\Core\Entity\Query\Sql\Condition
  4. 8.9.x core/lib/Drupal/Core/Entity/Query/Sql/pgsql/Condition.php \Drupal\Core\Entity\Query\Sql\pgsql\Condition
  5. 8.9.x core/lib/Drupal/Core/Entity/Query/Null/Condition.php \Drupal\Core\Entity\Query\Null\Condition
  6. 8.9.x core/lib/Drupal/Core/Entity/KeyValueStore/Query/Condition.php \Drupal\Core\Entity\KeyValueStore\Query\Condition
  7. 8.9.x core/lib/Drupal/Core/Condition/Annotation/Condition.php \Drupal\Core\Condition\Annotation\Condition
  8. 10 core/tests/fixtures/database_drivers/module/core_fake/src/Driver/Database/CoreFakeWithAllCustomClasses/Condition.php \Drupal\core_fake\Driver\Database\CoreFakeWithAllCustomClasses\Condition
  9. 10 core/lib/Drupal/Core/Config/Entity/Query/Condition.php \Drupal\Core\Config\Entity\Query\Condition
  10. 10 core/lib/Drupal/Core/Database/Query/Condition.php \Drupal\Core\Database\Query\Condition
  11. 10 core/lib/Drupal/Core/Entity/Query/Sql/Condition.php \Drupal\Core\Entity\Query\Sql\Condition
  12. 10 core/lib/Drupal/Core/Entity/Query/Sql/pgsql/Condition.php \Drupal\Core\Entity\Query\Sql\pgsql\Condition
  13. 10 core/lib/Drupal/Core/Entity/Query/Null/Condition.php \Drupal\Core\Entity\Query\Null\Condition
  14. 10 core/lib/Drupal/Core/Entity/KeyValueStore/Query/Condition.php \Drupal\Core\Entity\KeyValueStore\Query\Condition
  15. 10 core/lib/Drupal/Core/Condition/Attribute/Condition.php \Drupal\Core\Condition\Attribute\Condition
  16. 10 core/lib/Drupal/Core/Condition/Annotation/Condition.php \Drupal\Core\Condition\Annotation\Condition
  17. 11.x core/tests/fixtures/database_drivers/module/core_fake/src/Driver/Database/CoreFakeWithAllCustomClasses/Condition.php \Drupal\core_fake\Driver\Database\CoreFakeWithAllCustomClasses\Condition
  18. 11.x core/lib/Drupal/Core/Config/Entity/Query/Condition.php \Drupal\Core\Config\Entity\Query\Condition
  19. 11.x core/lib/Drupal/Core/Database/Query/Condition.php \Drupal\Core\Database\Query\Condition
  20. 11.x core/lib/Drupal/Core/Entity/Query/Sql/Condition.php \Drupal\Core\Entity\Query\Sql\Condition
  21. 11.x core/lib/Drupal/Core/Entity/Query/Sql/pgsql/Condition.php \Drupal\Core\Entity\Query\Sql\pgsql\Condition
  22. 11.x core/lib/Drupal/Core/Entity/Query/Null/Condition.php \Drupal\Core\Entity\Query\Null\Condition
  23. 11.x core/lib/Drupal/Core/Entity/KeyValueStore/Query/Condition.php \Drupal\Core\Entity\KeyValueStore\Query\Condition
  24. 11.x core/lib/Drupal/Core/Condition/Attribute/Condition.php \Drupal\Core\Condition\Attribute\Condition
  25. 11.x core/lib/Drupal/Core/Condition/Annotation/Condition.php \Drupal\Core\Condition\Annotation\Condition

Implements entity query conditions for SQL databases.

Hierarchy

Expanded class hierarchy of Condition

1 file declares its use of Condition
Condition.php in core/lib/Drupal/Core/Entity/Query/Sql/pgsql/Condition.php
7 string references to 'Condition'
ConditionAggregate::compile in core/lib/Drupal/Core/Entity/Query/Sql/ConditionAggregate.php
Compiles this conditional clause.
ConditionManager::getType in core/lib/Drupal/Core/Condition/ConditionManager.php
A string identifying the plugin type.
Connection::condition in core/lib/Drupal/Core/Database/Connection.php
Prepares and returns a CONDITION query object.
Connection::getDriverClass in core/lib/Drupal/Core/Database/Connection.php
Gets the driver-specific override class if any for the specified class.
ConnectionTest::providerGetDriverClass in core/tests/Drupal/Tests/Core/Database/ConnectionTest.php
Data provider for testGetDriverClass().

... See full list

File

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

Namespace

Drupal\Core\Entity\Query\Sql
View source
class Condition extends ConditionBase {
    
    /**
     * Whether this condition is nested inside an OR condition.
     *
     * @var bool
     */
    protected $nestedInsideOrCondition = FALSE;
    
    /**
     * The SQL entity query object this condition belongs to.
     *
     * @var \Drupal\Core\Entity\Query\Sql\Query
     */
    protected $query;
    
    /**
     * The current SQL query, set by parent condition compile() method calls.
     *
     * @var \Drupal\Core\Database\Query\SelectInterface
     */
    protected $sqlQuery;
    
    /**
     * {@inheritdoc}
     */
    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;
        $tables = $this->query
            ->getTables($sql_query);
        foreach ($this->conditions as $condition) {
            if ($condition['field'] instanceof ConditionInterface) {
                $sql_condition = $sql_query->getConnection()
                    ->condition($condition['field']->getConjunction());
                // Add the SQL query to the object before calling this method again.
                $condition['field']->sqlQuery = $sql_query;
                $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'], $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;
                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']);
                }
            }
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function exists($field, $langcode = NULL) {
        return $this->condition($field, NULL, 'IS NOT NULL', $langcode);
    }
    
    /**
     * {@inheritdoc}
     */
    public function notExists($field, $langcode = NULL) {
        return $this->condition($field, NULL, 'IS NULL', $langcode);
    }
    
    /**
     * Translates the string operators to SQL equivalents.
     *
     * @param array $condition
     *   The condition array.
     * @param \Drupal\Core\Database\Query\SelectInterface $sql_query
     *   Select query instance.
     * @param bool|null $case_sensitive
     *   If the condition should be case sensitive or not, NULL if the field does
     *   not define it.
     *
     * @see \Drupal\Core\Database\Query\ConditionInterface::condition()
     */
    public static function translateCondition(&$condition, SelectInterface $sql_query, $case_sensitive) {
        // // There is nothing we can do for IN ().
        if (is_array($condition['value'])) {
            return;
        }
        // Ensure that the default operator is set to simplify the cases below.
        if (empty($condition['operator'])) {
            $condition['operator'] = '=';
        }
        switch ($condition['operator']) {
            case '=':
                // If a field explicitly requests that queries should not be case
                // sensitive, use the LIKE operator, otherwise keep =.
                if ($case_sensitive === FALSE) {
                    $condition['value'] = $sql_query->escapeLike($condition['value']);
                    $condition['operator'] = 'LIKE';
                }
                break;
            case '<>':
                // If a field explicitly requests that queries should not be case
                // sensitive, use the NOT LIKE operator, otherwise keep <>.
                if ($case_sensitive === FALSE) {
                    $condition['value'] = $sql_query->escapeLike($condition['value']);
                    $condition['operator'] = 'NOT LIKE';
                }
                break;
            case 'STARTS_WITH':
                if ($case_sensitive) {
                    $condition['operator'] = 'LIKE BINARY';
                }
                else {
                    $condition['operator'] = 'LIKE';
                }
                $condition['value'] = $sql_query->escapeLike($condition['value']) . '%';
                break;
            case 'CONTAINS':
                if ($case_sensitive) {
                    $condition['operator'] = 'LIKE BINARY';
                }
                else {
                    $condition['operator'] = 'LIKE';
                }
                $condition['value'] = '%' . $sql_query->escapeLike($condition['value']) . '%';
                break;
            case 'ENDS_WITH':
                if ($case_sensitive) {
                    $condition['operator'] = 'LIKE BINARY';
                }
                else {
                    $condition['operator'] = 'LIKE';
                }
                $condition['value'] = '%' . $sql_query->escapeLike($condition['value']);
                break;
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
Condition::$nestedInsideOrCondition protected property Whether this condition is nested inside an OR condition.
Condition::$query protected property The SQL entity query object this condition belongs to. Overrides ConditionFundamentals::$query
Condition::$sqlQuery protected property The current SQL query, set by parent condition compile() method calls.
Condition::compile public function Compiles this conditional clause. Overrides ConditionInterface::compile
Condition::exists public function Queries for the existence of a field. Overrides ConditionInterface::exists
Condition::notExists public function Queries for the nonexistence of a field. Overrides ConditionInterface::notExists
Condition::translateCondition public static function Translates the string operators to SQL equivalents. 1
ConditionBase::condition public function Adds a condition. Overrides ConditionInterface::condition
ConditionFundamentals::$conditions protected property Array of conditions.
ConditionFundamentals::$conjunction protected property The conjunction of this condition group.
ConditionFundamentals::$namespaces protected property List of potential namespaces of the classes belonging to this condition.
ConditionFundamentals::conditions public function
ConditionFundamentals::count public function
ConditionFundamentals::getConjunction public function
ConditionFundamentals::__clone public function Implements the magic __clone function.
ConditionFundamentals::__construct public function Constructs a Condition object.

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