Browse Source

added documentation for chaining via() relations

see https://github.com/yiisoft/yii2/issues/10408#issuecomment-338881999
tags/2.0.13
Carsten Brandt 7 years ago
parent
commit
c4b3e102db
No known key found for this signature in database
GPG Key ID: BE4F41DE1DEEEED0
  1. 35
      docs/guide/db-active-record.md

35
docs/guide/db-active-record.md

@ -936,6 +936,41 @@ $items = $order->items;
```
### Chaining relation definitions via multiple tables <span id="multi-table-relations"></span>
Its further possible to define relations via multiple tables by chaining relation definitions using [[yii\db\ActiveQuery::via()|via()]].
Considering the examples above, we have classes `Customer`, `Order`, and `Item`.
We can add a relation to the `Customer` class that lists all items from all the orders they placed,
and name it `getPurchasedItems()`, the chaining of relations is show in the following code example:
```php
class Customer extends ActiveRecord
{
// ...
public function getPurchasedItems()
{
// customer's items, matching 'id' column of `Item` to 'item_id' in OrderItem
return $this->hasMany(Item::className(), ['id' => 'item_id'])
->via('orderItems');
}
public function getOrderItems()
{
// customer's order items, matching 'id' column of `Order` to 'order_id' in OrderItem
return $this->hasMany(OrderItem::className(), ['order_id' => 'id'])
->via('orders');
}
public function getOrders()
{
// same as above
return $this->hasMany(Order::className(), ['customer_id' => 'id']);
}
}
```
### Lazy Loading and Eager Loading <span id="lazy-eager-loading"></span>
In [Accessing Relational Data](#accessing-relational-data), we explained that you can access a relation property

Loading…
Cancel
Save