function ConditionInterface::condition

Same name in this branch
  1. 10 core/lib/Drupal/Core/Entity/Query/ConditionInterface.php \Drupal\Core\Entity\Query\ConditionInterface::condition()
Same name in other branches
  1. 9 core/lib/Drupal/Core/Database/Query/ConditionInterface.php \Drupal\Core\Database\Query\ConditionInterface::condition()
  2. 9 core/lib/Drupal/Core/Entity/Query/ConditionInterface.php \Drupal\Core\Entity\Query\ConditionInterface::condition()
  3. 8.9.x core/lib/Drupal/Core/Database/Query/ConditionInterface.php \Drupal\Core\Database\Query\ConditionInterface::condition()
  4. 8.9.x core/lib/Drupal/Core/Entity/Query/ConditionInterface.php \Drupal\Core\Entity\Query\ConditionInterface::condition()
  5. 11.x core/lib/Drupal/Core/Database/Query/ConditionInterface.php \Drupal\Core\Database\Query\ConditionInterface::condition()
  6. 11.x core/lib/Drupal/Core/Entity/Query/ConditionInterface.php \Drupal\Core\Entity\Query\ConditionInterface::condition()

Helper function: builds the most common conditional clauses.

This method takes 1 to 3 parameters.

If called with 1 parameter, it should be a ConditionInterface that in itself forms a valid where clause. Use e.g. to build clauses with nested ANDs and ORs.

If called with 2 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().

To improve readability, the operators EXISTS and NOT EXISTS have their own utility method defined.

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

\Drupal::database()->select('users')
    ->condition('name', $injected_connection->escapeLike($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

string|\Drupal\Core\Database\Query\ConditionInterface $field: The name of the field to check. This can also be QueryConditionInterface in itself. Use where(), if you would like to add a more complex condition involving operators or functions, or an already compiled condition.

string|int|array|\Drupal\Core\Database\Query\SelectInterface|null $value: The value to test the field against. In most cases, and depending on the operator, this will be a scalar or an array. As SQL accepts select queries on any place where a scalar value or set is expected, $value may also be a SelectInterface or an array of SelectInterfaces. If $operator is a unary operator, e.g. IS NULL, $value will be ignored and should be null. If the operator requires a subquery, e.g. EXISTS, the $field will be ignored and $value should be a SelectInterface object.

string|null $operator: The operator to use. Supported for all supported databases are at least:

  • The comparison operators =, <>, <, <=, >, >=.
  • The operators (NOT) BETWEEN, (NOT) IN, (NOT) EXISTS, (NOT) LIKE.

Other operators (e.g. LIKE BINARY) may or may not work. Defaults to =.

Return value

$this The called object.

Throws

\Drupal\Core\Database\InvalidQueryException If passed invalid arguments, such as an empty array as $value.

See also

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

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

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

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

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

2 methods override ConditionInterface::condition()
Condition::condition in core/lib/Drupal/Core/Database/Query/Condition.php
Helper function: builds the most common conditional clauses.
SelectExtender::condition in core/lib/Drupal/Core/Database/Query/SelectExtender.php
Helper function: builds the most common conditional clauses.

File

core/lib/Drupal/Core/Database/Query/ConditionInterface.php, line 73

Class

ConditionInterface
Interface for a conditional clause in a query.

Namespace

Drupal\Core\Database\Query

Code

public function condition($field, $value = NULL, $operator = '=');

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