Hi, I have the following models in my application: Address, City, Zip, Locale(=province) and Country. Where a Country has many Locale’s, a Locale has many Zip’s,…
What i want to achieve now is this:
$address = Address::model()->with('country')->findByPk(1);
echo $address->country->name;
Of course i want Yii only to load the data from the Address and the Country model. The data from the City, Zip and Locale aren’t needed so shouldn’t be loaded.
I tried to achieve this by defining my relations method of the Address model like this:
public function relations()
{
return array(
'city' => array(self::BELONGS_TO, 'City', 'id_city'),
'zip' => array(self::HAS_ONE, 'Zip', 'id_zip', 'through'=>'city'),
'locale' => array(self::HAS_ONE, 'Locale', 'id_locale', 'through'=>'zip'),
'country' => array(self::HAS_ONE, 'Country', 'id_country', 'through'=>'locale'),
);
}
Now Yii throws the following error:
The relation "locale" in active record class "Address" is specified with an invalid foreign key "id_locale".
There is no such column in the table "locale".
So apparently it is not possible to use the ‘through’ operator on a relation that already uses it.
How should this be done correctly in Yii?