# ActiveQueryTrait

> ActiveQueryTrait implements the common methods and properties for active record query classes.

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

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

Used by 1 type:

- [`ActiveQuery`](/docs/api/yii/db/activequery/index.md)

ActiveQueryTrait implements the common methods and properties for active record query classes.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| [`$asArray`](#property-asArray) | `bool` | Whether to return each record as an array. Default: `null`. | `ActiveQueryTrait` |
| <span id="property-modelClass"></span>`$modelClass` | `class-string<`[`ActiveRecordInterface`](/docs/api/yii/db/activerecordinterface/index.md)`>` | The name of the ActiveRecord class. Default: `null`. | `ActiveQueryTrait` |
| <span id="property-with"></span>`$with` | `array\|null` | A list of relations that this query should be performed with Default: `null`. | `ActiveQueryTrait` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`asArray()`](#method-asArray) | Sets the [`asArray()`](/docs/api/yii/db/activequerytrait/index.md#method-asArray) property. | `ActiveQueryTrait` |
| [`findWith()`](#method-findWith) | Finds records corresponding to one or multiple relations and populates them into the primary models. | `ActiveQueryTrait` |
| [`with()`](#method-with) | Specifies the relations with which this query should be performed. | `ActiveQueryTrait` |
| [`createModels()`](#method-createModels) protected | Converts found rows into model instances. | `ActiveQueryTrait` |

## Property details

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

```php
public bool $asArray = null
```

Whether to return each record as an array.

If false (default), an object
of [`$modelClass`](/docs/api/yii/db/activequerytrait/index.md#property-modelClass) will be created to represent each record.

## Method details

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

```php
public function asArray(bool $value = true): $this
```

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

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `bool` | Whether to return the query results in terms of arrays instead of Active Records. |

**Returns**

`$this`: The query object itself

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

```php
public function findWith(array $with, array &$models): mixed
```

Finds records corresponding to one or multiple relations and populates them into the primary models.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$with` | `array` | A list of relations that this query should be performed with. Please refer to [`with()`](/docs/api/yii/db/activequerytrait/index.md#method-with) for details about specifying this parameter. |
| `$models` | `array` | The primary models (can be either AR instances or arrays) |

**Returns**

`mixed`

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

```php
public function with(): $this
```

Specifies the relations with which this query should be performed.

The parameters to this method can be either one or multiple strings, or a single array
of relation names and the optional callbacks to customize the relations.

A relation name can refer to a relation defined in [`$modelClass`](/docs/api/yii/db/activequerytrait/index.md#property-modelClass)
or a sub-relation that stands for a relation of a related record.
For example, `orders.address` means the `address` relation defined
in the model class corresponding to the `orders` relation.

The following are some usage examples:

```
// find customers together with their orders and country
Customer::find()->with('orders', 'country')->all();
// find customers together with their orders and the orders' shipping address
Customer::find()->with('orders.address')->all();
// find customers together with their country and orders of status 1
Customer::find()->with([
    'orders' => function (\yii\db\ActiveQuery $query) {
        $query->andWhere('status = 1');
    },
    'country',
])->all();
```

You can call `with()` multiple times. Each call will add relations to the existing ones.
For example, the following two statements are equivalent:

```
Customer::find()->with('orders', 'country')->all();
Customer::find()->with('orders')->with('country')->all();
```

**Returns**

`$this`: The query object itself

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

```php
protected function createModels(array $rows, yii\db\Connection|null $db = null): array|yii\db\BaseActiveRecord[]
```

Converts found rows into model instances.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$rows` | `array` | The rows to be converted into model instances. Each array element represents a row of data. |
| `$db` | [`Connection`](/docs/api/yii/db/connection/index.md)`\|null` | The database connection used to retrieve the rows. |

**Returns**

`array|`[`BaseActiveRecord`](/docs/api/yii/db/baseactiverecord/index.md)`[]`: The model instances created from the rows. If [`asArray()`](/docs/api/yii/db/activequerytrait/index.md#method-asArray) is true, the rows will be returned as is.

**Since**

2.0.11