Trait yii\db\ActiveRelationTrait
Implemented by | kartik\tree\models\TreeQuery, yii\db\ActiveQuery |
---|---|
Available since version | 2.0 |
Source Code | https://github.com/yiisoft/yii2/blob/master/framework/db/ActiveRelationTrait.php |
ActiveRelationTrait implements the common methods and properties for active record relational queries.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$inverseOf | string | The name of the relation that is the inverse of this relation. | yii\db\ActiveRelationTrait |
$link | array | The columns of the primary and foreign tables that establish a relation. | yii\db\ActiveRelationTrait |
$modelClass | yii\db\ActiveRecord | yii\db\ActiveRelationTrait | |
$multiple | boolean | Whether this query represents a relation to more than one record. | yii\db\ActiveRelationTrait |
$primaryModel | yii\db\ActiveRecord | The primary model of a relational query. | yii\db\ActiveRelationTrait |
$via | array|object | The query associated with the junction table. | yii\db\ActiveRelationTrait |
Public Methods
Method | Description | Defined By |
---|---|---|
() | ActiveRecordInterface[] all($db = null) See yii\db\ActiveQueryInterface::all() for more info. | yii\db\ActiveRelationTrait |
__clone() | Clones internal objects. | yii\db\ActiveRelationTrait |
findFor() | Finds the related records for the specified primary record. | yii\db\ActiveRelationTrait |
inverseOf() | Sets the name of the relation that is the inverse of this relation. | yii\db\ActiveRelationTrait |
one() | See yii\db\ActiveQueryInterface::one() for more info. | yii\db\ActiveRelationTrait |
populateRelation() | Finds the related records and populates them into the primary models. | yii\db\ActiveRelationTrait |
via() | Specifies the relation associated with the junction table. | yii\db\ActiveRelationTrait |
Property Details
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().
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.
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 all(). If false, only the first row of the results will be retrieved using one().
The primary model of a relational query. This is used only in lazy loading with dynamic query options.
Method Details
ActiveRecordInterface[] all($db = null) See yii\db\ActiveQueryInterface::all() for more info.
ActiveRecordInterface[] all($db = null) See yii\db\ActiveQueryInterface::all() for more info.
public ( ) | ||
return |
---|
Clones internal objects.
public void __clone ( ) |
Finds the related records for the specified primary record.
This method is invoked when a relation of an ActiveRecord is being accessed lazily.
public mixed findFor ( $name, $model ) | ||
$name | string | The relation name |
$model | yii\db\ActiveRecordInterface|yii\db\BaseActiveRecord | The primary model |
return | mixed | The related record(s) |
---|---|---|
throws | yii\base\InvalidArgumentException | if the relation is invalid |
Sets the name of the relation that is the inverse of this relation.
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 yii\db\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.
public $this inverseOf ( $relationName ) | ||
$relationName | string | The name of the relation that is the inverse of this relation. |
return | $this | The relation object itself. |
---|
See yii\db\ActiveQueryInterface::one() for more info.
See yii\db\ActiveQueryInterface::one() for more info.
public yii\db\ActiveRecordInterface one ( $db ) | ||
$db | ||
return | yii\db\ActiveRecordInterface |
---|
Finds the related records and populates them into the primary models.
public array populateRelation ( $name, &$primaryModels ) | ||
$name | string | The relation name |
$primaryModels | array | Primary models |
return | array | The related models |
---|---|---|
throws | yii\base\InvalidConfigException | if $link is invalid |
Specifies the relation associated with the junction table.
Use this method to specify a pivot record/table when declaring a relation in the yii\db\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');
}
}
public $this via ( $relationName, callable $callable = null ) | ||
$relationName | string | The relation name. This refers to a relation declared in $primaryModel. |
$callable | callable|null | A PHP callback for customizing the relation associated with the junction table.
Its signature should be |
return | $this | The relation object itself. |
---|