View only items related to current id

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

How are you getting $region?

Are you using findAll()? Or finding a specific $region via find(), findByPk, etc?

Do a print_r of the objects and see what they contain.

Thanks for the reply, I’m not using any extra code in the view other than the third item of code below. Based on your question I think I need something? I assumed $region referred to the current object of the model (i.e. if I’m viewing region 5, then $region would contain the variable 5)?

I tried right now to use the following code in the region view


<?php


$region=Region::model()->findByPk(5);


$countries = $region->countrys;

        foreach($countries as $countrys) {

                echo $countries->name;

}


?>



Also tried with the print_r as suggested, still nothing appears

OK success I think, the code below works, but I don’t really know why


$region=Region::model()->findByPk( $_GET['id']);


$countries = $region->countrys;

        foreach($countries as $countrys) {

                echo $countrys->name;

                echo '<br />';



This is what I think is happening:

As suggested by dniznick above, the first line grabs the regions by the ID in the URL. The second line then finds all countrys for that ID and stores it in a variable. The foreach part iterates through then and prints the results. What I don’t fully understand is how the foreach works. Why does the $countries as $countrys work? Why does it need to be written this way? Sorry if this is a newbie question

Remember - you named your relation ‘countrys’. Therefore I propose you try:




$region=Region::model()->findByPk($_GET['id']); //or findByPk(5), doesn't matter


$countries = $region->countrys;

        foreach($countries as $someOtherName) {

                echo $SomeOtherName->name;

                echo '<br />';

Thanks very much, appreciated!