Hi all
I’m basically a new Yii user, with some programming background (functional, not OO) and so far I’ve been through Larry’s series, the blog demo and browsed some interesting forum topics. I’ve dived straight into building a small website, which has (amongst others) a page where you can view world regions (e.g. Asia, South America) and then drill into the countries in each of the regions. What I want to do on the Regions view is to show a list of countries for that region (ideally clickable), which I thought would be fairly straightforward. However, as you may guess from this posting, I haven’t been able to find a solution to this likely straightforward issue for more experience coders. I’ve managed to get other parts of the website working used code grabbed from the blog/Larry’s series, so I’m learning (slowly!). I’ve also tried searching, but I guess my issue is that I’m not sure what I need to search for (tried filter view, filter based on id, etc)
My models relations are setup as below:
Region Model relations()
'countrys' => array(self::HAS_MANY, 'Country', 'region_id'),
The region table basically has an id (autonumber) and name
Country Model relations()
'region' => array(self::BELONGS_TO, 'Region', 'region_id'),
The country table has an id (autonumber), name and region_id (FK to region)
I thought it would be as easy (as mentioned in the blog demo, here) to use the piece of code below, but it dosen’t work (I’ve tried various iterations of the below with no success, most get the error invalid foreach()
$countries = $region->country;
foreach($countries as $country) {
echo $countries->name;
}
Firstly, is this the optimal way to achieve this? If so, where am I going wrong?
Thanks in advance