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.
public hasOne ( 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. |