function Sql::addWhere

Same name and namespace in other branches
  1. 9 core/modules/views/src/Plugin/views/query/Sql.php \Drupal\views\Plugin\views\query\Sql::addWhere()
  2. 8.9.x core/modules/views/src/Plugin/views/query/Sql.php \Drupal\views\Plugin\views\query\Sql::addWhere()
  3. 10 core/modules/views/src/Plugin/views/query/Sql.php \Drupal\views\Plugin\views\query\Sql::addWhere()

Adds a simple WHERE clause to the query.

The caller is responsible for ensuring that all fields are fully qualified (TABLE.FIELD) and that the table already exists in the query.

The $field, $value and $operator arguments can also be passed in with a single DatabaseCondition object, like this:

$this->query
    ->addWhere($this->options['group'], $this->query
    ->getConnection()
    ->condition('OR')
    ->condition($field, $value, 'NOT IN')
    ->condition($field, $value, 'IS NULL'));

Parameters

$group: The WHERE group to add these to; groups are used to create AND/OR sections. Groups cannot be nested. Use 0 as the default group. If the group does not yet exist it will be created as an AND group.

$field: The name of the field to check.

$value: The value to test the field against. In most cases, this is a scalar. For more complex options, it is an array. The meaning of each element in the array is dependent on the $operator.

$operator: The comparison operator, such as =, <, or >=. It also accepts more complex options such as IN, LIKE, LIKE BINARY, or BETWEEN. Defaults to =. If $field is a string you have to use 'formula' here.

See also

\Drupal\Core\Database\Query\ConditionInterface::condition()

\Drupal\Core\Database\Query\Condition

File

core/modules/views/src/Plugin/views/query/Sql.php, line 935

Class

Sql
Views query plugin for an SQL query.

Namespace

Drupal\views\Plugin\views\query

Code

public function addWhere($group, $field, $value = NULL, $operator = NULL) {
    // Ensure all variants of 0 are actually 0. Thus '', 0 and NULL are all
    // the default group.
    if (empty($group)) {
        $group = 0;
    }
    // Check for a group.
    if (!isset($this->where[$group])) {
        $this->setWhereGroup('AND', $group);
    }
    $this->where[$group]['conditions'][] = [
        'field' => $field,
        'value' => $value,
        'operator' => $operator,
    ];
}

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