dropdownlist + AR + relations

I have 3 tables involved: Region(id, sort), Region_lang(id, name_ua, name_en), Country(id, region_id). For the Country model, I want to create a dropdown list with (Region.id, Region_lang.name_ua) for example, and I'm stuck on:



<?= CHtml::activeDropDownList($country, 'region_id', CHtml::listData(Region::model()->with('lang')->findAll(), 'id', 'name_ua'), array('empty'=>'All categories', 'submit'=>'')) ?>




<?php





class Region extends CActiveRecord


{


.......





	public function relations()


	{


		return array(


			'lang'=>array(self::BELONGS_TO, 'Region_lang', 'id'),


			'countries'=>array(self::HAS_MANY, 'Country', 'region_id')


		);


	}


.......


}


What output are you getting? Is it an error about $countries not being defined?

No, about "name_ua". To get to name_ua, I need to do $region->lang->name_ua. I managed to accomplish this task by adding the following function to Region model:



<?php


class Region


{


......


           public function getAllList()


	{


		$models = $this->with('lang')->findAll();


		$listData = array();


		


		foreach($models as $model)


		{


			$listData[$model->id] = $model->lang->name_ua;


		}


		


		return $listData;


	}


}


?>


And calling:



<?php echo CHtml::activeDropDownList($country, 'region_id', Region::model()->getAllList()) ?>


I think this is something that could be included in next releases in CHtml::listData - the ability to specify columns from relation tables.

maybe it was a typo in the post but Region should extend CActiveRecord or a child class in your case

Yeah, it's a typo, I was adding it manually, not Copy+Paste



<?php


class Region extends CActiveRecord


{


....


}


?>