ActiveQueryTrait
ActiveQueryTrait implements the common methods and properties for active record query classes.
Tags
Table of Contents
Properties
- $asArray : bool
- $modelClass : string
- $with : array<string|int, mixed>
Methods
- asArray() : $this
- Sets the [[asArray]] property.
- findWith() : mixed
- Finds records corresponding to one or multiple relations and populates them into the primary models.
- with() : $this
- Specifies the relations with which this query should be performed.
- createModels() : array<string|int, mixed>|array<string|int, ActiveRecord>
- Converts found rows into model instances.
- normalizeRelations() : array<string|int, ActiveQueryInterface>
Properties
$asArray
public
bool
$asArray
whether to return each record as an array. If false (default), an object of [[modelClass]] will be created to represent each record.
$modelClass
public
string
$modelClass
the name of the ActiveRecord class.
$with
public
array<string|int, mixed>
$with
a list of relations that this query should be performed with
Methods
asArray()
Sets the [[asArray]] property.
public
asArray([bool $value = true ]) : $this
Parameters
- $value : bool = true
-
whether to return the query results in terms of arrays instead of Active Records.
Return values
$this —the query object itself
findWith()
Finds records corresponding to one or multiple relations and populates them into the primary models.
public
findWith(array<string|int, mixed> $with, array<string|int, mixed>|array<string|int, ActiveRecord> &$models) : mixed
Parameters
- $with : array<string|int, mixed>
-
a list of relations that this query should be performed with. Please refer to [[with()]] for details about specifying this parameter.
- $models : array<string|int, mixed>|array<string|int, ActiveRecord>
-
the primary models (can be either AR instances or arrays)
with()
Specifies the relations with which this query should be performed.
public
with() : $this
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]]
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();
Return values
$this —the query object itself
createModels()
Converts found rows into model instances.
protected
createModels(array<string|int, mixed> $rows) : array<string|int, mixed>|array<string|int, ActiveRecord>
Parameters
- $rows : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>|array<string|int, ActiveRecord>normalizeRelations()
private
normalizeRelations(ActiveRecord $model, array<string|int, mixed> $with) : array<string|int, ActiveQueryInterface>
Parameters
- $model : ActiveRecord
- $with : array<string|int, mixed>