ActiveQueryTrait implements the common methods and properties for active record query classes.
Property DetailsWhether to return each record as an array. If false (default), an object of $modelClass will be created to represent each record.
The name of the ActiveRecord class.
A list of relations that this query should be performed with
Method DetailsSets the asArray() property.
public $this asArray ( $value = true ) $value booleanWhether to return the query results in terms of arrays instead of Active Records.
return $thisThe query object itself
public function asArray($value = true)
{
$this->asArray = $value;
return $this;
}
Converts found rows into model instances.
protected function createModels($rows)
{
if ($this->asArray) {
return $rows;
} else {
$models = [];
$class = $this->modelClass;
foreach ($rows as $row) {
$model = $class::instantiate($row);
$modelClass = get_class($model);
$modelClass::populateRecord($model, $row);
$models[] = $model;
}
return $models;
}
}
Finds records corresponding to one or multiple relations and populates them into the primary models.
public void findWith ( $with, &$models ) $with arrayA list of relations that this query should be performed with. Please refer to with() for details about specifying this parameter.
$models array|yii\db\ActiveRecord[]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) {
$modelClass = $this->modelClass;
$primaryModel = $modelClass::instance();
}
$relations = $this->normalizeRelations($primaryModel, $with);
foreach ($relations as $name => $relation) {
if ($relation->asArray === null) {
$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:
Customer::find()->with('orders', 'country')->all();
Customer::find()->with('orders.address')->all();
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 function with()
{
$with = func_get_args();
if (isset($with[0]) && is_array($with[0])) {
$with = $with[0];
}
if (empty($this->with)) {
$this->with = $with;
} elseif (!empty($with)) {
foreach ($with as $name => $value) {
if (is_int($name)) {
$this->with[] = $value;
} else {
$this->with[$name] = $value;
}
}
}
return $this;
}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4