public function SelectExtender::condition in Drupal 10
Same name and namespace in other branches
- 8 core/lib/Drupal/Core/Database/Query/SelectExtender.php \Drupal\Core\Database\Query\SelectExtender::condition()
- 9 core/lib/Drupal/Core/Database/Query/SelectExtender.php \Drupal\Core\Database\Query\SelectExtender::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|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.
Overrides ConditionInterface::condition
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 calls to SelectExtender::condition()
- SearchQuery::execute in core/
modules/ search/ src/ SearchQuery.php - Executes the search.
- SearchQuery::prepareAndNormalize in core/
modules/ search/ src/ SearchQuery.php - Prepares the query and calculates the normalization factor.
File
- core/
lib/ Drupal/ Core/ Database/ Query/ SelectExtender.php, line 107
Class
- SelectExtender
- The base extender class for Select queries.
Namespace
Drupal\Core\Database\QueryCode
public function condition($field, $value = NULL, $operator = '=') {
$this->query
->condition($field, $value, $operator);
return $this;
}