When you access the relation of the model, like $model->relationName, you gather access to the relationName model relations too, so things like $model->relationName->SomeRelationInRelationName->name is perfectly valid, as long as the relation "relationName and SomeRelationInRelationName" returns only a single instance of the related model.
In case these returns multiple instances(arrays), then you’ll have to loop through them.
I just tried your solution, and it works perfectly.
My version of the 3 tables is: Region / Province / City each related to the previous one, so province with region, and city with province.
I wanted to show region / province / city for any given city i search.
The solution was to create the relations like so:
'provincii'=>array(self::HAS_MANY, 'Provincie', 'id_regiune'),
'regiune'=>array(self::BELONGS_TO, 'Regiuni', 'id_regiune'),
'provincie'=>array(self::BELONGS_TO, 'Provincii', 'id_provincie'),
and on the view i put:
$orase = Orase::model()->findAll(array("condition"=>"nume_oras like '%".$_POST["oras"]."%'","order"=>"id_oras"));
//$orase = Orase::model()->findByPk(5);
foreach ($orase as $orase_gasite):
echo "Regiunea:".$orase_gasite->provincie->regiune->nume_regiune." Provincia:".$orase_gasite->provincie->nume_provincie." Oras:".$orase_gasite->nume_oras."<br>";
But what i am not sure, is if i did the search correctly, and if there is another way to perform this search.
One field where the user inserts the city he is looking for, and then the search form returns the results.
I have created on the view page a form with the action in index (the form is on the front page), but i am sure there is a better way than hardcoding the form in that file.