function Condition::condition

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Database/Query/Condition.php \Drupal\Core\Database\Query\Condition::condition()
  2. 8.9.x core/lib/Drupal/Core/Database/Query/Condition.php \Drupal\Core\Database\Query\Condition::condition()
  3. 10 core/lib/Drupal/Core/Database/Query/Condition.php \Drupal\Core\Database\Query\Condition::condition()

Overrides ConditionInterface::condition

4 calls to Condition::condition()
Condition::exists in core/lib/Drupal/Core/Database/Query/Condition.php
Sets a condition that the specified subquery returns values.
Condition::isNotNull in core/lib/Drupal/Core/Database/Query/Condition.php
Sets a condition that the specified field be NOT NULL.
Condition::isNull in core/lib/Drupal/Core/Database/Query/Condition.php
Sets a condition that the specified field be NULL.
Condition::notExists in core/lib/Drupal/Core/Database/Query/Condition.php
Sets a condition that the specified subquery returns no values.

File

core/lib/Drupal/Core/Database/Query/Condition.php, line 100

Class

Condition
Generic class for a series of conditions in a query.

Namespace

Drupal\Core\Database\Query

Code

public function condition($field, $value = NULL, $operator = '=') {
    if (empty($operator)) {
        $operator = '=';
    }
    if (empty($value) && is_array($value)) {
        throw new InvalidQueryException(sprintf("Query condition '%s %s ()' cannot be empty.", $field, $operator));
    }
    if (is_array($value) && in_array($operator, [
        '=',
        '<',
        '>',
        '<=',
        '>=',
        'IS NULL',
        'IS NOT NULL',
    ], TRUE)) {
        if (count($value) > 1) {
            $value = implode(', ', $value);
            throw new InvalidQueryException(sprintf("Query condition '%s %s %s' must have an array compatible operator.", $field, $operator, $value));
        }
        else {
            throw new InvalidQueryException('Calling ' . __METHOD__ . '() without an array compatible operator is not supported. See https://www.drupal.org/node/3350985');
        }
    }
    $this->conditions[] = [
        'field' => $field,
        'value' => $value,
        'operator' => $operator,
    ];
    $this->changed = TRUE;
    return $this;
}

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