ActiveRelationTrait
ActiveRelationTrait implements the common methods and properties for active record relational queries.
Tags
Table of Contents
Properties
- $inverseOf : string
- $link : array<string|int, mixed>
- $modelClass : ActiveRecord
- $multiple : bool
- $primaryModel : ActiveRecord
- $via : array<string|int, mixed>|object
- $viaMap : mixed
Methods
- __clone() : mixed
- Clones internal objects.
- all() : array<string|int, ActiveRecordInterface>
- findFor() : mixed
- Finds the related records for the specified primary record.
- inverseOf() : $this
- Sets the name of the relation that is the inverse of this relation.
- one() : ActiveRecordInterface|array<string|int, mixed>|null
- populateRelation() : array<string|int, mixed>
- Finds the related records and populates them into the primary models.
- via() : $this
- Specifies the relation associated with the junction table.
- addInverseRelations() : mixed
- If applicable, populate the query's primary model into the related records' inverse relationship.
- buildBuckets() : array<string|int, mixed>
- filterByModels() : mixed
- findJunctionRows() : array<string|int, mixed>
- getModelKey() : string|false
- indexBuckets() : array<string|int, mixed>
- Indexes buckets by column name.
- mapVia() : array<string|int, mixed>
- normalizeModelKey() : string
- populateInverseRelation() : mixed
- prefixKeyColumns() : array<string|int, mixed>
Properties
$inverseOf
public
string
$inverseOf
the name of the relation that is the inverse of this relation.
For example, an order has a customer, which means the inverse of the "customer" relation
is the "orders", and the inverse of the "orders" relation is the "customer".
If this property is set, the primary record(s) will be referenced through the specified relation.
For example, $customer->orders[0]->customer
and $customer
will be the same object,
and accessing the customer of an order will not trigger new DB query.
This property is only used in relational context.
Tags
$link
public
array<string|int, mixed>
$link
the columns of the primary and foreign tables that establish a relation. The array keys must be columns of the table for this relation, and the array values must be the corresponding columns from the primary table. Do not prefix or quote the column names as this will be done automatically by Yii. This property is only used in relational context.
$modelClass
public
ActiveRecord
$modelClass
$multiple
public
bool
$multiple
whether this query represents a relation to more than one record. This property is only used in relational context. If true, this relation will populate all query results into AR instances using [[Query::all()|all()]]. If false, only the first row of the results will be retrieved using [[Query::one()|one()]].
$primaryModel
public
ActiveRecord
$primaryModel
the primary model of a relational query. This is used only in lazy loading with dynamic query options.
$via
public
array<string|int, mixed>|object
$via
the query associated with the junction table. Please call [[via()]] to set this property instead of directly setting it. This property is only used in relational context.
Tags
$viaMap
private
mixed
$viaMap
Methods
__clone()
Clones internal objects.
public
__clone() : mixed
all()
public
all([mixed $db = null ]) : array<string|int, ActiveRecordInterface>
See [[ActiveQueryInterface::all()]] for more info.
Parameters
- $db : mixed = null
Return values
array<string|int, ActiveRecordInterface>findFor()
Finds the related records for the specified primary record.
public
findFor(string $name, ActiveRecordInterface|BaseActiveRecord $model) : mixed
This method is invoked when a relation of an ActiveRecord is being accessed lazily.
Parameters
- $name : string
-
the relation name
- $model : ActiveRecordInterface|BaseActiveRecord
-
the primary model
Tags
Return values
mixed —the related record(s)
inverseOf()
Sets the name of the relation that is the inverse of this relation.
public
inverseOf(string $relationName) : $this
For example, a customer has orders, which means the inverse of the "orders" relation is the "customer".
If this property is set, the primary record(s) will be referenced through the specified relation.
For example, $customer->orders[0]->customer
and $customer
will be the same object,
and accessing the customer of an order will not trigger a new DB query.
Use this method when declaring a relation in the [[ActiveRecord]] class, e.g. in Customer model:
public function getOrders()
{
return $this->hasMany(Order::class, ['customer_id' => 'id'])->inverseOf('customer');
}
This also may be used for Order model, but with caution:
public function getCustomer()
{
return $this->hasOne(Customer::class, ['id' => 'customer_id'])->inverseOf('orders');
}
in this case result will depend on how order(s) was loaded. Let's suppose customer has several orders. If only one order was loaded:
$orders = Order::find()->where(['id' => 1])->all();
$customerOrders = $orders[0]->customer->orders;
variable $customerOrders
will contain only one order. If orders was loaded like this:
$orders = Order::find()->with('customer')->where(['customer_id' => 1])->all();
$customerOrders = $orders[0]->customer->orders;
variable $customerOrders
will contain all orders of the customer.
Parameters
- $relationName : string
-
the name of the relation that is the inverse of this relation.
Return values
$this —the relation object itself.
one()
public
one([mixed $db = null ]) : ActiveRecordInterface|array<string|int, mixed>|null
See [[ActiveQueryInterface::one()]] for more info.
Parameters
- $db : mixed = null
Return values
ActiveRecordInterface|array<string|int, mixed>|nullpopulateRelation()
Finds the related records and populates them into the primary models.
public
populateRelation(string $name, array<string|int, mixed> &$primaryModels) : array<string|int, mixed>
Parameters
- $name : string
-
the relation name
- $primaryModels : array<string|int, mixed>
-
primary models
Tags
Return values
array<string|int, mixed> —the related models
via()
Specifies the relation associated with the junction table.
public
via(string $relationName[, callable|null $callable = null ]) : $this
Use this method to specify a pivot record/table when declaring a relation in the [[ActiveRecord]] class:
class Order extends ActiveRecord
{
public function getOrderItems() {
return $this->hasMany(OrderItem::class, ['order_id' => 'id']);
}
public function getItems() {
return $this->hasMany(Item::class, ['id' => 'item_id'])
->via('orderItems');
}
}
Parameters
- $relationName : string
-
the relation name. This refers to a relation declared in [[primaryModel]].
- $callable : callable|null = null
-
a PHP callback for customizing the relation associated with the junction table. Its signature should be
function($query)
, where$query
is the query to be customized.
Return values
$this —the relation object itself.
addInverseRelations()
If applicable, populate the query's primary model into the related records' inverse relationship.
private
addInverseRelations(array<string|int, mixed> &$result) : mixed
Parameters
- $result : array<string|int, mixed>
-
the array of related records as generated by [[populate()]]
Tags
buildBuckets()
private
buildBuckets(array<string|int, mixed> $models, array<string|int, mixed> $link[, array<string|int, mixed>|null $viaModels = null ][, self|null $viaQuery = null ][, bool $checkMultiple = true ]) : array<string|int, mixed>
Parameters
- $models : array<string|int, mixed>
- $link : array<string|int, mixed>
- $viaModels : array<string|int, mixed>|null = null
- $viaQuery : self|null = null
- $checkMultiple : bool = true
Return values
array<string|int, mixed>filterByModels()
private
filterByModels(array<string|int, mixed> $models) : mixed
Parameters
- $models : array<string|int, mixed>
findJunctionRows()
private
findJunctionRows(array<string|int, mixed> $primaryModels) : array<string|int, mixed>
Parameters
- $primaryModels : array<string|int, mixed>
-
either array of AR instances or arrays
Return values
array<string|int, mixed>getModelKey()
private
getModelKey(ActiveRecordInterface|array<string|int, mixed> $model, array<string|int, mixed> $attributes) : string|false
Parameters
- $model : ActiveRecordInterface|array<string|int, mixed>
- $attributes : array<string|int, mixed>
Return values
string|falseindexBuckets()
Indexes buckets by column name.
private
indexBuckets(array<string|int, mixed> $buckets, string|callable $indexBy) : array<string|int, mixed>
Parameters
- $buckets : array<string|int, mixed>
- $indexBy : 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.
Return values
array<string|int, mixed>mapVia()
private
mapVia(array<string|int, mixed> $map, array<string|int, mixed> $viaMap) : array<string|int, mixed>
Parameters
- $map : array<string|int, mixed>
- $viaMap : array<string|int, mixed>
Return values
array<string|int, mixed>normalizeModelKey()
private
normalizeModelKey(mixed $value) : string
Parameters
- $value : mixed
-
raw key value. Since 2.0.40 non-string values must be convertible to string (like special objects for cross-DBMS relations, for example:
|MongoId
).
Return values
string —normalized key value.
populateInverseRelation()
private
populateInverseRelation(array<string|int, ActiveRecordInterface> &$primaryModels, array<string|int, ActiveRecordInterface> $models, string $primaryName, string $name) : mixed
Parameters
- $primaryModels : array<string|int, ActiveRecordInterface>
-
primary models
- $models : array<string|int, ActiveRecordInterface>
-
models
- $primaryName : string
-
the primary relation name
- $name : string
-
the relation name
prefixKeyColumns()
private
prefixKeyColumns(array<string|int, mixed> $attributes) : array<string|int, mixed>
Parameters
- $attributes : array<string|int, mixed>
-
the attributes to prefix