PHP Trait yii\db\ActiveRelationTrait

Since: 2.0
Author: Qiang Xue ([email protected])
Author: Carsten Brandt ([email protected])
Show file Open project: yiisoft/yii2

Public Properties

Property Type Description
$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.
$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.
$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 the primary model of a relational query. This is used only in lazy loading with dynamic query options.
$via the query associated with the junction table. Please call ActiveRelationTrait::via to set this property instead of directly setting it. This property is only used in relational context.

Public Methods

Method Description
__clone ( ) Clones internal objects.
findFor ( string $name, yii\db\ActiveRecordInterface | BaseActiveRecord $model ) : mixed Finds the related records for the specified primary record.
inverseOf ( string $relationName ) Sets the name of the relation that is the inverse of this relation.
populateRelation ( string $name, array &$primaryModels ) : array Finds the related records and populates them into the primary models.
via ( string $relationName, callable $callable = null ) Specifies the relation associated with the junction table.

Private Methods

Method Description
addInverseRelations ( array &$result ) If applicable, populate the query's primary model into the related records' inverse relationship
buildBuckets ( array $models, array $link, array $viaModels = null, array $viaLink = null, boolean $checkMultiple = true ) : array
filterByModels ( array $models )
findJunctionRows ( array $primaryModels ) : array
getModelKey ( yii\db\ActiveRecordInterface | array $model, array $attributes ) : string
indexBuckets ( array $buckets, $indexBy ) : array Indexes buckets by column name.
normalizeModelKey ( mixed $value ) : string
populateInverseRelation ( yii\db\ActiveRecordInterface[] &$primaryModels, yii\db\ActiveRecordInterface[] $models, string $primaryName, string $name )
prefixKeyColumns ( array $attributes ) : array

Method Details

__clone() public method

Clones internal objects.
public __clone ( )

findFor() public method

This method is invoked when a relation of an ActiveRecord is being accessed in a lazy fashion.
public findFor ( string $name, yii\db\ActiveRecordInterface | BaseActiveRecord $model ) : mixed
$name string the relation name
$model yii\db\ActiveRecordInterface | BaseActiveRecord the primary model
return mixed the related record(s)

inverseOf() public method

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 a new DB query. Use this method when declaring a relation in the ActiveRecord class: php public function getOrders() { return $this->hasMany(Order::className(), ['customer_id' => 'id'])->inverseOf('customer'); }
public inverseOf ( string $relationName )
$relationName string the name of the relation that is the inverse of this relation.

populateRelation() public method

Finds the related records and populates them into the primary models.
public populateRelation ( string $name, array &$primaryModels ) : array
$name string the relation name
$primaryModels array primary models
return array the related models

via() public method

Use this method to specify a pivot record/table when declaring a relation in the ActiveRecord class: php class Order extends ActiveRecord { public function getOrderItems() { return $this->hasMany(OrderItem::className(), ['order_id' => 'id']); } public function getItems() { return $this->hasMany(Item::className(), ['id' => 'item_id']) ->via('orderItems'); } }
public via ( string $relationName, callable $callable = null )
$relationName string the relation name. This refers to a relation declared in [[primaryModel]].
$callable callable 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.

Property Details

$inverseOf public property

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.
See also: inverseOf()
public $inverseOf

$multiple public property

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()]].
public $multiple

$primaryModel public property

the primary model of a relational query. This is used only in lazy loading with dynamic query options.
public $primaryModel

$via public property

the query associated with the junction table. Please call ActiveRelationTrait::via to set this property instead of directly setting it. This property is only used in relational context.
See also: via()
public $via