hasOne() public method
Declares a has-one
relation.
The declaration is returned in terms of a relational yii\db\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:
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 yii\db\ActiveQuery to further customize the relation.
public yii\db\ActiveQueryInterface hasOne ( $class, $link ) | ||
---|---|---|
$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 |
return | yii\db\ActiveQueryInterface |
The relational query object. |
Please login to continue.