QueryTrait
API reference › yii › db › QueryTrait
Used by 1 type
The BaseQuery trait represents the minimum method set of a database Query.
It is supposed to be used in a class that implements the QueryInterface.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$emulateExecution | bool | Whether to emulate the actual query execution, returning empty or false results. Default: false. Available since 2.0.11. | QueryTrait |
$indexBy | string| | The name of the column by which the query results should be indexed by. Default: null. | QueryTrait |
$limit | int|ExpressionInterface| | Maximum number of records to be returned. Default: null. | QueryTrait |
$offset | int|ExpressionInterface| | Zero-based offset from where the records are to be returned. Default: null. | QueryTrait |
$orderBy | array| | How to sort the query results. Default: null. | QueryTrait |
$where | string|ExpressionInterface| | Query condition. Default: null. | QueryTrait |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
addOrderBy() | Adds additional ORDER BY columns to the query. | QueryTrait |
andFilterWhere() | Adds an additional WHERE condition to the existing one but ignores empty operands. | QueryTrait |
andWhere() | Adds an additional WHERE condition to the existing one. | QueryTrait |
emulateExecution() | Sets whether to emulate query execution, preventing any interaction with data storage. | QueryTrait |
filterWhere() | Sets the WHERE part of the query but ignores empty operands. | QueryTrait |
indexBy() | Sets the indexBy() property. | QueryTrait |
limit() | Sets the LIMIT part of the query. | QueryTrait |
offset() | Sets the OFFSET part of the query. | QueryTrait |
orderBy() | Sets the ORDER BY part of the query. | QueryTrait |
orFilterWhere() | Adds an additional WHERE condition to the existing one but ignores empty operands. | QueryTrait |
orWhere() | Adds an additional WHERE condition to the existing one. | QueryTrait |
where() | Sets the WHERE part of the query. | QueryTrait |
filterCondition() protected | Removes empty operands from the given query condition. | QueryTrait |
isEmpty() protected | Returns a value indicating whether the give value is “empty”. | QueryTrait |
normalizeOrderBy() protected | Normalizes format of ORDER BY data. | QueryTrait |
Property details
Section titled “Property details”$indexBy
Section titled “$indexBy ”public string|callable|null $indexBy = nullThe name of the column by which the query results should be indexed by.
This can also be a callable (e.g. anonymous function) that returns the index value based on the given
row data. For more details, see indexBy(). This property is only used by all().
$limit
Section titled “$limit ”public int|yii\db\ExpressionInterface|null $limit = nullMaximum number of records to be returned.
May be an instance of ExpressionInterface.
If not set or less than 0, it means no limit.
$offset
Section titled “$offset ”public int|yii\db\ExpressionInterface|null $offset = nullZero-based offset from where the records are to be returned.
May be an instance of ExpressionInterface. If not set or less than 0, it means starting from the beginning.
$orderBy
Section titled “$orderBy ”public array|null $orderBy = nullHow to sort the query results.
This is used to construct the ORDER BY clause in a SQL statement.
The array keys are the columns to be sorted by, and the array values are the corresponding sort directions which
can be either SORT_ASC
or SORT_DESC.
The array may also contain ExpressionInterface objects. If that is the case, the expressions
will be converted into strings without any change.
$where
Section titled “$where ”public string|array|yii\db\ExpressionInterface|null $where = nullQuery condition.
This refers to the WHERE clause in a SQL statement.
For example, ['age' => 31, 'team' => 1].
Method details
Section titled “Method details”addOrderBy()
Section titled “addOrderBy() ”public function addOrderBy(string|array|yii\db\ExpressionInterface $columns): $thisAdds additional ORDER BY columns to the query.
Parameters
| Parameter | Type | Description |
|---|---|---|
$columns | string|ExpressionInterface | The columns (and the directions) to be ordered by. Columns can be specified in either a string (e.g. “id ASC, name DESC”) or an array (e.g. ['id' => SORT_ASC,). The method will automatically quote the column names unless a column contains some parenthesis (which means the column contains a DB expression). Note that if your order-by is an expression containing commas, you should always use an array to represent the order-by information. Otherwise, the method will not be able to correctly determine the order-by columns. Since version 2.0.7, an ExpressionInterface object can be passed to specify the ORDER BY part explicitly in plain SQL. |
Returns
$this: The query object itself
See also
andFilterWhere()
Section titled “andFilterWhere() ”public function andFilterWhere(array $condition): $thisAdds an additional WHERE condition to the existing one but ignores empty operands.
The new condition and the existing one will be joined using the ‘AND’ operator.
This method is similar to andWhere(). The main difference is that this method will
remove empty query operands. As a result, this method is best suited
for building query conditions based on filter values entered by users.
Parameters
| Parameter | Type | Description |
|---|---|---|
$condition | array | The new WHERE condition. Please refer to where() on how to specify this parameter. |
Returns
$this: The query object itself
See also
andWhere()
Section titled “andWhere() ”public function andWhere(string|array|yii\db\ExpressionInterface $condition): $thisAdds an additional WHERE condition to the existing one.
The new condition and the existing one will be joined using the ‘AND’ operator.
Parameters
| Parameter | Type | Description |
|---|---|---|
$condition | string|ExpressionInterface | The new WHERE condition. Please refer to where() on how to specify this parameter. |
Returns
$this: The query object itself
See also
emulateExecution()
Section titled “emulateExecution() ”public function emulateExecution(bool $value = true): $thisSets whether to emulate query execution, preventing any interaction with data storage.
After this mode is enabled, methods, returning query results like QueryInterface::one(),
QueryInterface::all(), QueryInterface::exists() and so on, will return empty or false values.
You should use this method in case your program logic indicates query should not return any results, like
in case you set false where condition like 0=1.
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | bool | Whether to prevent query execution. |
Returns
$this: The query object itself.
Since
2.0.11
filterWhere()
Section titled “filterWhere() ”public function filterWhere(array $condition): $thisSets the WHERE part of the query but ignores empty operands.
This method is similar to where(). The main difference is that this method will
remove empty query operands. As a result, this method is best suited
for building query conditions based on filter values entered by users.
The following code shows the difference between this method and where():
// WHERE `age`=:age$query->filterWhere(['name' => null, 'age' => 20]);// WHERE `age`=:age$query->where(['age' => 20]);// WHERE `name` IS NULL AND `age`=:age$query->where(['name' => null, 'age' => 20]);Note that unlike where(), you cannot pass binding parameters to this method.
Parameters
| Parameter | Type | Description |
|---|---|---|
$condition | array | The conditions that should be put in the WHERE part. See where() on how to specify this parameter. |
Returns
$this: The query object itself
See also
indexBy()
Section titled “indexBy() ”public function indexBy(string|callable $column): $thisSets the indexBy() property.
Parameters
| Parameter | Type | Description |
|---|---|---|
$column | string| | The name of the column by which the query results should be indexed by. This can also be a callable (e.g. anonymous function) that returns the index value based on the given row data. The signature of the callable should be: function ($row) { // return the index value corresponding to $row } |
Returns
$this: The query object itself
limit()
Section titled “limit() ”public function limit(int|yii\db\ExpressionInterface|null $limit): $thisSets the LIMIT part of the query.
Parameters
| Parameter | Type | Description |
|---|---|---|
$limit | int|ExpressionInterface| | The limit. Use null or negative value to disable limit. |
Returns
$this: The query object itself
offset()
Section titled “offset() ”public function offset(int|yii\db\ExpressionInterface|null $offset): $thisSets the OFFSET part of the query.
Parameters
| Parameter | Type | Description |
|---|---|---|
$offset | int|ExpressionInterface| | The offset. Use null or negative value to disable offset. |
Returns
$this: The query object itself
orderBy()
Section titled “orderBy() ”public function orderBy(string|array|yii\db\ExpressionInterface|null $columns): $thisSets the ORDER BY part of the query.
Parameters
| Parameter | Type | Description |
|---|---|---|
$columns | string|ExpressionInterface| | The columns (and the directions) to be ordered by. Columns can be specified in either a string (e.g. "id ASC,) or an array (e.g. ['id' => SORT_ASC,). The method will automatically quote the column names unless a column contains some parenthesis (which means the column contains a DB expression). Note that if your order-by is an expression containing commas, you should always use an array to represent the order-by information. Otherwise, the method will not be able to correctly determine the order-by columns. Since version 2.0.7, an ExpressionInterface object can be passed to specify the ORDER BY part explicitly in plain SQL. |
Returns
$this: The query object itself
See also
orFilterWhere()
Section titled “orFilterWhere() ”public function orFilterWhere(array $condition): $thisAdds an additional WHERE condition to the existing one but ignores empty operands.
The new condition and the existing one will be joined using the ‘OR’ operator.
This method is similar to orWhere(). The main difference is that this method will
remove empty query operands. As a result, this method is best suited
for building query conditions based on filter values entered by users.
Parameters
| Parameter | Type | Description |
|---|---|---|
$condition | array | The new WHERE condition. Please refer to where() on how to specify this parameter. |
Returns
$this: The query object itself
See also
orWhere()
Section titled “orWhere() ”public function orWhere(string|array|yii\db\ExpressionInterface $condition): $thisAdds an additional WHERE condition to the existing one.
The new condition and the existing one will be joined using the ‘OR’ operator.
Parameters
| Parameter | Type | Description |
|---|---|---|
$condition | string|ExpressionInterface | The new WHERE condition. Please refer to where() on how to specify this parameter. |
Returns
$this: The query object itself
See also
where()
Section titled “where() ”public function where(string|array|yii\db\ExpressionInterface $condition): $thisSets the WHERE part of the query.
See QueryInterface::where() for detailed documentation.
Parameters
| Parameter | Type | Description |
|---|---|---|
$condition | string|ExpressionInterface | The conditions that should be put in the WHERE part. |
Returns
$this: The query object itself
See also
filterCondition()
Section titled “filterCondition() ”protected function filterCondition(array $condition): arrayRemoves empty operands from the given query condition.
Parameters
| Parameter | Type | Description |
|---|---|---|
$condition | array | The original condition |
Returns
array: The condition with empty operands removed.
Throws
yii\base\NotSupportedException: if the condition operator is not supported
isEmpty()
Section titled “isEmpty() ”protected function isEmpty(mixed $value): boolReturns a value indicating whether the give value is “empty”.
The value is considered “empty”, if one of the following conditions is satisfied:
- it is
null, - an empty string (
''), - a string containing only whitespace characters,
- or an empty array.
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | mixed |
Returns
bool: If the value is empty
normalizeOrderBy()
Section titled “normalizeOrderBy() ”protected function normalizeOrderBy(array|string|yii\db\ExpressionInterface|null $columns): arrayNormalizes format of ORDER BY data.
Parameters
| Parameter | Type | Description |
|---|---|---|
$columns | array|ExpressionInterface| | The columns value to normalize. See orderBy() and addOrderBy(). |
Returns
array