The declaration is returned in terms of a relational ActiveQuery instance
through which the related record can be queried and retrieved back.
A has-many relation means that there are multiple related records matching
the criteria set by this relation, e.g., a customer has many orders.
For example, to declare the orders relation for Customer class, we can write
the following code in the Customer class:
php
public function getOrders()
{
return $this->hasMany(Order::className(), ['customer_id' => 'id']);
}
Note that in the above, the 'customer_id' key in the $link parameter refers to
an attribute name in the related class Order, while the 'id' value refers to
an attribute name in the current AR class.
Call methods declared in ActiveQuery to further customize the relation.
public hasMany ( string $class, array $link ) : yii\db\ActiveQueryInterface | ||
$class | string | the class name of the related record |
$link | array | the primary-foreign key constraint. The keys of the array refer to the attributes of the record associated with the `$class` model, while the values of the array refer to the corresponding attributes in **this** AR class. |
return | yii\db\ActiveQueryInterface | the relational query object. |