You are here

public function Condition::condition in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Database/Query/Condition.php \Drupal\Core\Database\Query\Condition::condition()

Helper function: builds the most common conditional clauses.

This method can take a variable number of parameters. If called with two parameters, they are taken as $field and $value with $operator having a value of =.

Do not use this method to test for NULL values. Instead, use QueryConditionInterface::isNull() or QueryConditionInterface::isNotNull().

Drupal considers LIKE case insensitive and the following is often used to tell the database that case insensitive equivalence is desired:

db_select('users')
  ->condition('name', db_like($name), 'LIKE');

Use 'LIKE BINARY' instead of 'LIKE' for case sensitive queries.

Note: When using MySQL, the exact behavior also depends on the used collation. if the field is set to binary, then a LIKE condition will also be case sensitive and when a case insensitive collation is used, the = operator will also be case insensitive.

Parameters

$field: The name of the field to check. If you would like to add a more complex condition involving operators or functions, use where().

$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 =.

Return value

\Drupal\Core\Database\Query\ConditionInterface The called object.

Overrides ConditionInterface::condition

See also

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

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

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 71
Contains \Drupal\Core\Database\Query\Condition.

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));
  }
  $this->conditions[] = array(
    'field' => $field,
    'value' => $value,
    'operator' => $operator,
  );
  $this->changed = TRUE;
  return $this;
}