PHP Trait yii\db\QueryTrait

It is supposed to be used in a class that implements the QueryInterface.
Since: 2.0
Author: Qiang Xue ([email protected])
Author: Carsten Brandt ([email protected])
Show file Open project: yiisoft/yii2

Public Properties

Property Type Description
$emulateExecution whether to emulate the actual query execution, returning empty or false results.
$indexBy string | callable
$limit maximum number of records to be returned. If not set or less than 0, it means no limit.
$offset zero-based offset from where the records are to be returned. If not set or less than 0, it means starting from the beginning.
$orderBy 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 [[Expression]] objects. If that is the case, the expressions will be converted into strings without any change.
$where query condition. This refers to the WHERE clause in a SQL statement. For example, ['age' => 31, 'team' => 1].

Public Methods

Method Description
addOrderBy ( string | array | yii\db\Expression $columns ) Adds additional ORDER BY columns to the query.
andFilterWhere ( array $condition ) Adds an additional WHERE condition to the existing one but ignores [[isEmpty()|empty operands]].
andWhere ( string | array $condition ) Adds an additional WHERE condition to the existing one.
emulateExecution ( boolean $value = true ) Sets whether to emulate query execution, preventing any interaction with data storage.
filterWhere ( array $condition ) Sets the WHERE part of the query but ignores [[isEmpty()|empty operands]].
indexBy ( string | callable $column ) Sets the [[indexBy]] property.
limit ( integer $limit ) Sets the LIMIT part of the query.
offset ( integer $offset ) Sets the OFFSET part of the query.
orFilterWhere ( array $condition ) Adds an additional WHERE condition to the existing one but ignores [[isEmpty()|empty operands]].
orWhere ( string | array $condition ) Adds an additional WHERE condition to the existing one.
orderBy ( string | array | yii\db\Expression $columns ) Sets the ORDER BY part of the query.
where ( string | array $condition ) Sets the WHERE part of the query.

Protected Methods

Method Description
filterCondition ( array $condition ) : array Removes [[isEmpty()|empty operands]] from the given query condition.
isEmpty ( mixed $value ) : boolean Returns a value indicating whether the give value is "empty".
normalizeOrderBy ( array | string | yii\db\Expression $columns ) : array Normalizes format of ORDER BY data

Method Details

addOrderBy() public method

Adds additional ORDER BY columns to the query.
See also: orderBy()
public addOrderBy ( string | array | yii\db\Expression $columns )
$columns string | array | yii\db\Expression 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, '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 [[Expression]] object can be passed to specify the ORDER BY part explicitly in plain SQL.

andFilterWhere() public method

The new condition and the existing one will be joined using the 'AND' operator. This method is similar to QueryTrait::andWhere. The main difference is that this method will remove [[isEmpty()|empty query operands]]. As a result, this method is best suited for building query conditions based on filter values entered by users.
See also: filterWhere()
See also: orFilterWhere()
public andFilterWhere ( array $condition )
$condition array the new WHERE condition. Please refer to [[where()]] on how to specify this parameter.

andWhere() public method

The new condition and the existing one will be joined using the 'AND' operator.
See also: where()
See also: orWhere()
public andWhere ( string | array $condition )
$condition string | array the new WHERE condition. Please refer to [[where()]] on how to specify this parameter.

emulateExecution() public method

After this mode is enabled, methods, returning query results like [[one()]], [[all()]], [[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.
Since: 2.0.11
public emulateExecution ( boolean $value = true )
$value boolean whether to prevent query execution.

filterCondition() protected method

Removes [[isEmpty()|empty operands]] from the given query condition.
protected filterCondition ( array $condition ) : array
$condition array the original condition
return array the condition with [[isEmpty()|empty operands]] removed.

filterWhere() public method

This method is similar to QueryTrait::where. The main difference is that this method will remove [[isEmpty()|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 QueryTrait::where: php 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 QueryTrait::where, you cannot pass binding parameters to this method.
See also: where()
See also: andFilterWhere()
See also: orFilterWhere()
public filterWhere ( array $condition )
$condition array the conditions that should be put in the WHERE part. See [[where()]] on how to specify this parameter.

indexBy() public method

Sets the [[indexBy]] property.
public indexBy ( string | callable $column )
$column string | callable 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: ```php function ($row) { // return the index value corresponding to $row } ```

isEmpty() protected method

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.
protected isEmpty ( mixed $value ) : boolean
$value mixed
return boolean if the value is empty

limit() public method

Sets the LIMIT part of the query.
public limit ( integer $limit )
$limit integer the limit. Use null or negative value to disable limit.

normalizeOrderBy() protected method

Normalizes format of ORDER BY data
protected normalizeOrderBy ( array | string | yii\db\Expression $columns ) : array
$columns array | string | yii\db\Expression the columns value to normalize. See [[orderBy]] and [[addOrderBy]].
return array

offset() public method

Sets the OFFSET part of the query.
public offset ( integer $offset )
$offset integer the offset. Use null or negative value to disable offset.

orFilterWhere() public method

The new condition and the existing one will be joined using the 'OR' operator. This method is similar to QueryTrait::orWhere. The main difference is that this method will remove [[isEmpty()|empty query operands]]. As a result, this method is best suited for building query conditions based on filter values entered by users.
See also: filterWhere()
See also: andFilterWhere()
public orFilterWhere ( array $condition )
$condition array the new WHERE condition. Please refer to [[where()]] on how to specify this parameter.

orWhere() public method

The new condition and the existing one will be joined using the 'OR' operator.
See also: where()
See also: andWhere()
public orWhere ( string | array $condition )
$condition string | array the new WHERE condition. Please refer to [[where()]] on how to specify this parameter.

orderBy() public method

Sets the ORDER BY part of the query.
See also: addOrderBy()
public orderBy ( string | array | yii\db\Expression $columns )
$columns string | array | yii\db\Expression 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, '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 [[Expression]] object can be passed to specify the ORDER BY part explicitly in plain SQL.

where() public method

See [[QueryInterface::where()]] for detailed documentation.
See also: andWhere()
See also: orWhere()
public where ( string | array $condition )
$condition string | array the conditions that should be put in the WHERE part.

Property Details

$emulateExecution public property

whether to emulate the actual query execution, returning empty or false results.
See also: emulateExecution()
Since: 2.0.11
public $emulateExecution

$indexBy public property

public string|callable $indexBy
return string | callable

$limit public property

maximum number of records to be returned. If not set or less than 0, it means no limit.
public $limit

$offset public property

zero-based offset from where the records are to be returned. If not set or less than 0, it means starting from the beginning.
public $offset

$orderBy public property

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 [[Expression]] objects. If that is the case, the expressions will be converted into strings without any change.
public $orderBy

$where public property

query condition. This refers to the WHERE clause in a SQL statement. For example, ['age' => 31, 'team' => 1].
See also: where() for valid syntax on specifying this value.
public $where