# QueryTrait

> The BaseQuery trait represents the minimum method set of a database Query.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [db](/docs/api/yii/db/index.md) › QueryTrait

| trait | `yii\db\QueryTrait` |
| --- | --- |
| Since | 2.0 |
| Source | [`framework/db/QueryTrait.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/db/QueryTrait.php#L22) |

Used by 1 type:

- [`Query`](/docs/api/yii/db/query/index.md)

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`](/docs/api/yii/db/queryinterface/index.md).

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-emulateExecution"></span>`$emulateExecution` | `bool` | Whether to emulate the actual query execution, returning empty or false results. Default: `false`. Available since 2.0.11. | `QueryTrait` |
| [`$indexBy`](#property-indexBy) | `string\|callable\|null` | The name of the column by which the query results should be indexed by. Default: `null`. | `QueryTrait` |
| [`$limit`](#property-limit) | `int\|`[`ExpressionInterface`](/docs/api/yii/db/expressioninterface/index.md)`\|null` | Maximum number of records to be returned. Default: `null`. | `QueryTrait` |
| [`$offset`](#property-offset) | `int\|`[`ExpressionInterface`](/docs/api/yii/db/expressioninterface/index.md)`\|null` | Zero-based offset from where the records are to be returned. Default: `null`. | `QueryTrait` |
| [`$orderBy`](#property-orderBy) | `array\|null` | How to sort the query results. Default: `null`. | `QueryTrait` |
| [`$where`](#property-where) | `string\|array\|`[`ExpressionInterface`](/docs/api/yii/db/expressioninterface/index.md)`\|null` | Query condition. Default: `null`. | `QueryTrait` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`addOrderBy()`](#method-addOrderBy) | Adds additional ORDER BY columns to the query. | `QueryTrait` |
| [`andFilterWhere()`](#method-andFilterWhere) | Adds an additional WHERE condition to the existing one but ignores [empty operands](/docs/api/yii/db/querytrait/index.md#method-isEmpty). | `QueryTrait` |
| [`andWhere()`](#method-andWhere) | Adds an additional WHERE condition to the existing one. | `QueryTrait` |
| [`emulateExecution()`](#method-emulateExecution) | Sets whether to emulate query execution, preventing any interaction with data storage. | `QueryTrait` |
| [`filterWhere()`](#method-filterWhere) | Sets the WHERE part of the query but ignores [empty operands](/docs/api/yii/db/querytrait/index.md#method-isEmpty). | `QueryTrait` |
| [`indexBy()`](#method-indexBy) | Sets the [`indexBy()`](/docs/api/yii/db/querytrait/index.md#method-indexBy) property. | `QueryTrait` |
| [`limit()`](#method-limit) | Sets the LIMIT part of the query. | `QueryTrait` |
| [`offset()`](#method-offset) | Sets the OFFSET part of the query. | `QueryTrait` |
| [`orderBy()`](#method-orderBy) | Sets the ORDER BY part of the query. | `QueryTrait` |
| [`orFilterWhere()`](#method-orFilterWhere) | Adds an additional WHERE condition to the existing one but ignores [empty operands](/docs/api/yii/db/querytrait/index.md#method-isEmpty). | `QueryTrait` |
| [`orWhere()`](#method-orWhere) | Adds an additional WHERE condition to the existing one. | `QueryTrait` |
| [`where()`](#method-where) | Sets the WHERE part of the query. | `QueryTrait` |
| [`filterCondition()`](#method-filterCondition) protected | Removes [empty operands](/docs/api/yii/db/querytrait/index.md#method-isEmpty) from the given query condition. | `QueryTrait` |
| [`isEmpty()`](#method-isEmpty) protected | Returns a value indicating whether the give value is "empty". | `QueryTrait` |
| [`normalizeOrderBy()`](#method-normalizeOrderBy) protected | Normalizes format of ORDER BY data. | `QueryTrait` |

## Property details

### $indexBy <span id="property-indexBy"></span>

```php
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()`](/docs/api/yii/db/querytrait/index.md#method-indexBy). This property is only used by [all()](/docs/api/yii/db/queryinterface/index.md#method-all).

### $limit <span id="property-limit"></span>

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

Maximum number of records to be returned.

May be an instance of [`ExpressionInterface`](/docs/api/yii/db/expressioninterface/index.md).
If not set or less than 0, it means no limit.

### $offset <span id="property-offset"></span>

```php
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`](/docs/api/yii/db/expressioninterface/index.md). If not set or less than 0, it means starting from the beginning.

### $orderBy <span id="property-orderBy"></span>

```php
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](https://www.php.net/manual/en/array.constants.php#constant.sort-asc)
or [SORT_DESC](https://www.php.net/manual/en/array.constants.php#constant.sort-desc).
The array may also contain [`ExpressionInterface`](/docs/api/yii/db/expressioninterface/index.md) objects. If that is the case, the expressions
will be converted into strings without any change.

### $where <span id="property-where"></span>

```php
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]`.

## Method details

### addOrderBy() <span id="method-addOrderBy"></span>

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

Adds additional ORDER BY columns to the query.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$columns` | `string\|array\|`[`ExpressionInterface`](/docs/api/yii/db/expressioninterface/index.md) | 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 [`ExpressionInterface`](/docs/api/yii/db/expressioninterface/index.md) object can be passed to specify the ORDER BY part explicitly in plain SQL. |

**Returns**

`$this`: The query object itself

**See also**

- [`orderBy()`](/docs/api/yii/db/querytrait/index.md#method-orderBy)

### andFilterWhere() <span id="method-andFilterWhere"></span>

```php
public function andFilterWhere(array $condition): $this
```

Adds an additional WHERE condition to the existing one but ignores [empty operands](/docs/api/yii/db/querytrait/index.md#method-isEmpty).

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

This method is similar to [`andWhere()`](/docs/api/yii/db/querytrait/index.md#method-andWhere). The main difference is that this method will
remove [empty query operands](/docs/api/yii/db/querytrait/index.md#method-isEmpty). 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()`](/docs/api/yii/db/querytrait/index.md#method-where) on how to specify this parameter. |

**Returns**

`$this`: The query object itself

**See also**

- [`filterWhere()`](/docs/api/yii/db/querytrait/index.md#method-filterWhere)
- [`orFilterWhere()`](/docs/api/yii/db/querytrait/index.md#method-orFilterWhere)

### andWhere() <span id="method-andWhere"></span>

```php
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**

| Parameter | Type | Description |
| --- | --- | --- |
| `$condition` | `string\|array\|`[`ExpressionInterface`](/docs/api/yii/db/expressioninterface/index.md) | The new WHERE condition. Please refer to [`where()`](/docs/api/yii/db/querytrait/index.md#method-where) on how to specify this parameter. |

**Returns**

`$this`: The query object itself

**See also**

- [`where()`](/docs/api/yii/db/querytrait/index.md#method-where)
- [`orWhere()`](/docs/api/yii/db/querytrait/index.md#method-orWhere)

### emulateExecution() <span id="method-emulateExecution"></span>

```php
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()`](/docs/api/yii/db/queryinterface/index.md#method-one),
[`QueryInterface::all()`](/docs/api/yii/db/queryinterface/index.md#method-all), [`QueryInterface::exists()`](/docs/api/yii/db/queryinterface/index.md#method-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() <span id="method-filterWhere"></span>

```php
public function filterWhere(array $condition): $this
```

Sets the WHERE part of the query but ignores [empty operands](/docs/api/yii/db/querytrait/index.md#method-isEmpty).

This method is similar to [`where()`](/docs/api/yii/db/querytrait/index.md#method-where). The main difference is that this method will
remove [empty query operands](/docs/api/yii/db/querytrait/index.md#method-isEmpty). 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()`](/docs/api/yii/db/querytrait/index.md#method-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()`](/docs/api/yii/db/querytrait/index.md#method-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()`](/docs/api/yii/db/querytrait/index.md#method-where) on how to specify this parameter. |

**Returns**

`$this`: The query object itself

**See also**

- [`where()`](/docs/api/yii/db/querytrait/index.md#method-where)
- [`andFilterWhere()`](/docs/api/yii/db/querytrait/index.md#method-andFilterWhere)
- [`orFilterWhere()`](/docs/api/yii/db/querytrait/index.md#method-orFilterWhere)

### indexBy() <span id="method-indexBy"></span>

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

Sets the [`indexBy()`](/docs/api/yii/db/querytrait/index.md#method-indexBy) property.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$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: function ($row) { // return the index value corresponding to $row } |

**Returns**

`$this`: The query object itself

### limit() <span id="method-limit"></span>

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

Sets the LIMIT part of the query.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$limit` | `int\|`[`ExpressionInterface`](/docs/api/yii/db/expressioninterface/index.md)`\|null` | The limit. Use null or negative value to disable limit. |

**Returns**

`$this`: The query object itself

### offset() <span id="method-offset"></span>

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

Sets the OFFSET part of the query.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$offset` | `int\|`[`ExpressionInterface`](/docs/api/yii/db/expressioninterface/index.md)`\|null` | The offset. Use null or negative value to disable offset. |

**Returns**

`$this`: The query object itself

### orderBy() <span id="method-orderBy"></span>

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

Sets the ORDER BY part of the query.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$columns` | `string\|array\|`[`ExpressionInterface`](/docs/api/yii/db/expressioninterface/index.md)`\|null` | 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 [`ExpressionInterface`](/docs/api/yii/db/expressioninterface/index.md) object can be passed to specify the ORDER BY part explicitly in plain SQL. |

**Returns**

`$this`: The query object itself

**See also**

- [`addOrderBy()`](/docs/api/yii/db/querytrait/index.md#method-addOrderBy)

### orFilterWhere() <span id="method-orFilterWhere"></span>

```php
public function orFilterWhere(array $condition): $this
```

Adds an additional WHERE condition to the existing one but ignores [empty operands](/docs/api/yii/db/querytrait/index.md#method-isEmpty).

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

This method is similar to [`orWhere()`](/docs/api/yii/db/querytrait/index.md#method-orWhere). The main difference is that this method will
remove [empty query operands](/docs/api/yii/db/querytrait/index.md#method-isEmpty). 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()`](/docs/api/yii/db/querytrait/index.md#method-where) on how to specify this parameter. |

**Returns**

`$this`: The query object itself

**See also**

- [`filterWhere()`](/docs/api/yii/db/querytrait/index.md#method-filterWhere)
- [`andFilterWhere()`](/docs/api/yii/db/querytrait/index.md#method-andFilterWhere)

### orWhere() <span id="method-orWhere"></span>

```php
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**

| Parameter | Type | Description |
| --- | --- | --- |
| `$condition` | `string\|array\|`[`ExpressionInterface`](/docs/api/yii/db/expressioninterface/index.md) | The new WHERE condition. Please refer to [`where()`](/docs/api/yii/db/querytrait/index.md#method-where) on how to specify this parameter. |

**Returns**

`$this`: The query object itself

**See also**

- [`where()`](/docs/api/yii/db/querytrait/index.md#method-where)
- [`andWhere()`](/docs/api/yii/db/querytrait/index.md#method-andWhere)

### where() <span id="method-where"></span>

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

Sets the WHERE part of the query.

See [`QueryInterface::where()`](/docs/api/yii/db/queryinterface/index.md#method-where) for detailed documentation.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$condition` | `string\|array\|`[`ExpressionInterface`](/docs/api/yii/db/expressioninterface/index.md) | The conditions that should be put in the WHERE part. |

**Returns**

`$this`: The query object itself

**See also**

- [`andWhere()`](/docs/api/yii/db/querytrait/index.md#method-andWhere)
- [`orWhere()`](/docs/api/yii/db/querytrait/index.md#method-orWhere)

### filterCondition() <span id="method-filterCondition"></span>

```php
protected function filterCondition(array $condition): array
```

Removes [empty operands](/docs/api/yii/db/querytrait/index.md#method-isEmpty) from the given query condition.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$condition` | `array` | The original condition |

**Returns**

`array`: The condition with [empty operands](/docs/api/yii/db/querytrait/index.md#method-isEmpty) removed.

**Throws**

- [`yii\base\NotSupportedException`](/docs/api/yii/base/notsupportedexception/index.md): if the condition operator is not supported

### isEmpty() <span id="method-isEmpty"></span>

```php
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**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `mixed` |  |

**Returns**

`bool`: If the value is empty

### normalizeOrderBy() <span id="method-normalizeOrderBy"></span>

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

Normalizes format of ORDER BY data.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$columns` | `array\|string\|`[`ExpressionInterface`](/docs/api/yii/db/expressioninterface/index.md)`\|null` | The columns value to normalize. See [`orderBy()`](/docs/api/yii/db/querytrait/index.md#method-orderBy) and [`addOrderBy()`](/docs/api/yii/db/querytrait/index.md#method-addOrderBy). |

**Returns**

`array`