My database was modeled like this (reducing tables into its keys):
City
- id
- region_id
Region
- id
- country_id
Country
- id
I would like to do something like that:
$city = City::model()->with('region', 'country')->findByPk(1);
echo "$city - {$city->region->acronym}, {$city->country->name}";
But I can’t figure out how to build the ‘[font=“Courier New”]country[/font]’ relation, since it would be a BELONGS_TO with [font=“Courier New”]‘through’ => ‘region’[/font] option.
Any help or idea would be greatly appreciated.
abennouna
(Abennouna)
March 29, 2012, 9:17pm
2
I haven’t tried it yet, but give it a try
$city = City::model()->with(array('region' => array('with' => 'country')))->findByPk(1);
Inspired from:
http://www.yiiframework.com/forum/index.php/topic/23985-nested-relational-criteria-in-search/
And maybe test with simple BELONGS_TO relations (city/region and region/country), and use
echo "$city - {$city->region->acronym}, {$city->region->country->name}";
redguy
(Maciej Lizewski)
April 18, 2012, 4:19pm
3
also:
$city = City::model()->with(array('region', 'region.country'))->findByPk(1);
should work