The declaration is returned in terms of a relational
ActiveQuery instance
through which the related record can be queried and retrieved back.
A
has-one relation means that there is at most one related record matching
the criteria set by this relation, e.g., a customer has one country.
For example, to declare the
country relation for
Customer class, we can write
the following code in the
Customer class:
php
public function getCountry()
{
return $this->hasOne(Country::className(), ['id' => 'country_id']);
}
Note that in the above, the 'id' key in the
$link parameter refers to an attribute name
in the related class
Country, while the 'country_id' value refers to an attribute name
in the current AR class.
Call methods declared in
ActiveQuery to further customize the relation.