Skip to content
PHP Ecosystem
Explore

QueryTrait

API reference › yii › db › QueryTrait

traityii\db\QueryTrait
Since2.0
Sourceframework/db/QueryTrait.php
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.

PropertyTypeDescriptionDefined by
$emulateExecutionboolWhether to emulate the actual query execution, returning empty or false results. Default: false. Available since 2.0.11.QueryTrait
$indexBystring|callable|nullThe name of the column by which the query results should be indexed by. Default: null.QueryTrait
$limitint|ExpressionInterface|nullMaximum number of records to be returned. Default: null.QueryTrait
$offsetint|ExpressionInterface|nullZero-based offset from where the records are to be returned. Default: null.QueryTrait
$orderByarray|nullHow to sort the query results. Default: null.QueryTrait
$wherestring|array|ExpressionInterface|nullQuery condition. Default: null.QueryTrait
MethodDescriptionDefined 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() protectedRemoves empty operands from the given query condition.QueryTrait
isEmpty() protectedReturns a value indicating whether the give value is “empty”.QueryTrait
normalizeOrderBy() protectedNormalizes format of ORDER BY data.QueryTrait
public string|callable|null $indexBy = null

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. For more details, see indexBy(). This property is only used by all().

public int|yii\db\ExpressionInterface|null $limit = null

Maximum number of records to be returned.

May be an instance of ExpressionInterface. If not set or less than 0, it means no limit.

public int|yii\db\ExpressionInterface|null $offset = null

Zero-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.

public array|null $orderBy = null

How 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.

public string|array|yii\db\ExpressionInterface|null $where = null

Query condition.

This refers to the WHERE clause in a SQL statement. For example, ['age' => 31, 'team' => 1].

public function addOrderBy(string|array|yii\db\ExpressionInterface $columns): $this

Adds additional ORDER BY columns to the query.

Parameters

ParameterTypeDescription
$columnsstring|array|ExpressionInterfaceThe 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, 'name' => SORT_DESC]). 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

public function andFilterWhere(array $condition): $this

Adds 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

ParameterTypeDescription
$conditionarrayThe new WHERE condition. Please refer to where() on how to specify this parameter.

Returns

$this: The query object itself

See also

public function andWhere(string|array|yii\db\ExpressionInterface $condition): $this

Adds an additional WHERE condition to the existing one.

The new condition and the existing one will be joined using the ‘AND’ operator.

Parameters

ParameterTypeDescription
$conditionstring|array|ExpressionInterfaceThe new WHERE condition. Please refer to where() on how to specify this parameter.

Returns

$this: The query object itself

See also

public function emulateExecution(bool $value = true): $this

Sets 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

ParameterTypeDescription
$valueboolWhether to prevent query execution.

Returns

$this: The query object itself.

Since

2.0.11

public function filterWhere(array $condition): $this

Sets 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

ParameterTypeDescription
$conditionarrayThe 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

public function indexBy(string|callable $column): $this

Sets the indexBy() property.

Parameters

ParameterTypeDescription
$columnstring|callableThe 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

public function limit(int|yii\db\ExpressionInterface|null $limit): $this

Sets the LIMIT part of the query.

Parameters

ParameterTypeDescription
$limitint|ExpressionInterface|nullThe limit. Use null or negative value to disable limit.

Returns

$this: The query object itself

public function offset(int|yii\db\ExpressionInterface|null $offset): $this

Sets the OFFSET part of the query.

Parameters

ParameterTypeDescription
$offsetint|ExpressionInterface|nullThe offset. Use null or negative value to disable offset.

Returns

$this: The query object itself

public function orderBy(string|array|yii\db\ExpressionInterface|null $columns): $this

Sets the ORDER BY part of the query.

Parameters

ParameterTypeDescription
$columnsstring|array|ExpressionInterface|nullThe 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, 'name' => SORT_DESC]). 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

public function orFilterWhere(array $condition): $this

Adds 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

ParameterTypeDescription
$conditionarrayThe new WHERE condition. Please refer to where() on how to specify this parameter.

Returns

$this: The query object itself

See also

public function orWhere(string|array|yii\db\ExpressionInterface $condition): $this

Adds an additional WHERE condition to the existing one.

The new condition and the existing one will be joined using the ‘OR’ operator.

Parameters

ParameterTypeDescription
$conditionstring|array|ExpressionInterfaceThe new WHERE condition. Please refer to where() on how to specify this parameter.

Returns

$this: The query object itself

See also

public function where(string|array|yii\db\ExpressionInterface $condition): $this

Sets the WHERE part of the query.

See QueryInterface::where() for detailed documentation.

Parameters

ParameterTypeDescription
$conditionstring|array|ExpressionInterfaceThe conditions that should be put in the WHERE part.

Returns

$this: The query object itself

See also

protected function filterCondition(array $condition): array

Removes empty operands from the given query condition.

Parameters

ParameterTypeDescription
$conditionarrayThe original condition

Returns

array: The condition with empty operands removed.

Throws

protected function isEmpty(mixed $value): bool

Returns 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

ParameterTypeDescription
$valuemixed

Returns

bool: If the value is empty

protected function normalizeOrderBy(array|string|yii\db\ExpressionInterface|null $columns): array

Normalizes format of ORDER BY data.

Parameters

ParameterTypeDescription
$columnsarray|string|ExpressionInterface|nullThe columns value to normalize. See orderBy() and addOrderBy().

Returns

array