Yiibooster Dropdownlistrow

Hi All,

Started my first project with Yii and YiiBooster and have a question regarding dropDownListRow.

In _form.php I have the following:




<?php echo $form->($model, 'NationalityID', $model->getNationalities()); ?>



In the model I have:




	public function getNationalities()

	{

		

		//$rawData = Nationality::model()->findAll();

		$rawData=Yii::app()->db->createCommand('SELECT * FROM tbl_nationality')->queryAll();

		

		$dataProvider = new CArrayDataProvider($rawData, array(

		    'id'=>'someid',

		    'keyField' => 'ID',

		    'sort'=>array(

		        'attributes'=>array('Nationality'),

		    ),

		));

		$val = $dataProvider->getData();

		return $val;

	}



How do I specify the format of the array to return. With the "findAll()" function to populate rawData I get an error, and with the "queryAll()" it works but it returns this array:




Array ( [0] => Array ( [ID] => 1 [Nationality] => Algerian ) [1] => Array ( [ID] => 4 [Nationality] => Belgian ) [2] => Array ( [ID] => 2 [Nationality] => French ) [3] => Array ( [ID] => 3 [Nationality] => Italian ) )



I need some guidance…

first you have to set the relations from the base model




	public function relations()

	{


		return array(

			'nationality' => array(self::BELONGS_TO, 'Nationality', 'nationality'), //nationality is the foreign key from this table

		);

	}




if you created a foreign key relation prior creating the model (using gii) it automatically generates it.

view




<?php echo $form->dropDownList($model,'nationality', 

CHtml::listData(Nationality::model()->findAll(), 'id', 'nationality'), //id is the primary key of Nationality table

array('empty'=>'Select here...')); ?>



you don’t need to create a new function to access all the records in table nationality

hope this helps

Like this?


<?php echo $form->dropDownListRow($model , 'NationalityID', CHtml::listData(Nationality::model()->findAll(), 'ID', 'Nationality')); ?>

Fellow member answered before.

That worked like a charm. I had the foreign key set properly in the model relations, but the CHtml listdata escaped me.

Sincere thanks to both of you