Trait yii\db\ActiveQueryTrait
| Implemented by | yii\ |
|---|---|
| Available since version | 2.0 |
| Source Code | https://github.com/yiisoft/yii2/blob/master/framework/db/ActiveQueryTrait.php |
ActiveQueryTrait implements the common methods and properties for active record query classes.
Public Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $asArray | boolean|null | Whether to return each record as an array. | yii\ |
| $modelClass | class-string<\yii\db\ActiveRecordInterface> | The name of the ActiveRecord class. | yii\ |
| $with | array|null | A list of relations that this query should be performed with | yii\ |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| asArray() | Sets the asArray() property. | yii\ |
| findWith() | Finds records corresponding to one or multiple relations and populates them into the primary models. | yii\ |
| with() | Specifies the relations with which this query should be performed. | yii\ |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| createModels() | Converts found rows into model instances. | yii\ |
Property Details
Whether to return each record as an array. If true, each record is returned as an array. If false, an object of $modelClass will be created to represent each record. Defaults to null, which means that a relation query used for eager loading inherits this setting from the primary query. If no setting is inherited, objects are returned.
The name of the ActiveRecord class.
Method Details
Sets the asArray() property.
| public $this asArray ( boolean $value = true ) | ||
| $value | boolean |
Whether to return the query results in terms of arrays instead of Active Records. |
| return | $this |
The query object itself |
|---|---|---|
public function asArray($value = true)
{
$this->asArray = $value;
return $this;
}
Converts found rows into model instances.
| protected array|yii\ | ||
| $rows | array |
The rows to be converted into model instances. Each array element represents a row of data. |
| $db | yii\ |
The database connection used to retrieve the rows. |
| return | array|yii\ |
The model instances created from the rows. If asArray() is true, the rows will be returned as is. |
|---|---|---|
protected function createModels($rows, $db = null)
{
if ($this->asArray) {
return $rows;
} else {
$models = [];
/** @var class-string<BaseActiveRecord> $class */
$class = $this->modelClass;
foreach ($rows as $row) {
$model = $class::instantiate($row);
if ($model instanceof ActiveRecord) {
$model::populateRecord($model, $row, $db);
} else {
$model::populateRecord($model, $row);
}
$models[] = $model;
}
return $models;
}
}
Finds records corresponding to one or multiple relations and populates them into the primary models.
| public mixed findWith ( array $with, array &$models ) | ||
| $with | array |
A list of relations that this query should be performed with. Please refer to with() for details about specifying this parameter. |
| $models | array |
The primary models (can be either AR instances or arrays) |
public function findWith($with, &$models)
{
if (empty($models)) {
return;
}
$primaryModel = reset($models);
if (!$primaryModel instanceof ActiveRecordInterface) {
/** @var ActiveRecordInterface $modelClass */
$modelClass = $this->modelClass;
$primaryModel = $modelClass::instance();
}
$relations = $this->normalizeRelations($primaryModel, $with);
/** @var ActiveQuery $relation */
foreach ($relations as $name => $relation) {
if ($relation->asArray === null) {
// inherit asArray from primary query
$relation->asArray($this->asArray);
}
$relation->populateRelation($name, $models);
}
}
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
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();
| public $this with ( ) | ||
| return | $this |
The query object itself |
|---|---|---|
public function with()
{
$with = func_get_args();
if (isset($with[0]) && is_array($with[0])) {
// the parameter is given as an array
$with = $with[0];
}
if (empty($this->with)) {
$this->with = $with;
} elseif (!empty($with)) {
foreach ($with as $name => $value) {
if (is_int($name)) {
// repeating relation is fine as normalizeRelations() handle it well
$this->with[] = $value;
} else {
$this->with[$name] = $value;
}
}
}
return $this;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.