Creo que buscas esto, un poco tarde…
http://www.yiiframew…ive-record.html
Declaring Relations
To work with relational data using Active Record, you first need to declare relations in Active Record classes. The task is as simple as declaring arelation method for every interested relation, like the following,
class Customer extends ActiveRecord
{
public function getOrders()
{
return $this->hasMany(Order::className(), ['customer_id' => 'id']);
}
}
class Order extends ActiveRecord
{
public function getCustomer()
{
return $this->hasOne(Customer::className(), ['id' => 'customer_id']);
}
}
Relations via a Junction Table
In database modelling, when the multiplicity between two related tables is many-to-many, a junction table is usually introduced. For example, theorder table and the item table may be related via a junction table named order_item. One order will then correspond to multiple order items, while one product item will also correspond to multiple order items.
When declaring such relations, you would call either via() or viaTable() to specify the junction table. The difference between via() and viaTable()is that the former specifies the junction table in terms of an existing relation name while the latter directly the junction table. For example,
class Order extends ActiveRecord
{
public function getItems()
{
return $this->hasMany(Item::className(), ['id' => 'item_id'])
->viaTable('order_item', ['order_id' => 'id']);
}
}
or alternatively,
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');
}
}
The usage of relations declared with a junction table is the same as that of normal relations. For example,
// SELECT * FROM `order` WHERE `id` = 100
$order = Order::findOne(100);
// SELECT * FROM `order_item` WHERE `order_id` = 100
// SELECT * FROM `item` WHERE `item_id` IN (...)
// returns an array of Item objects
$items = $order->items;