Populating dropdown list with CActiverecord data

In controller I have

$model=new Userregistration('register');






				// form inputs are valid, do something here





		$countries = Allcountries::model()->findAll(array('order' => 'country ASC'));


		$user = Userregistration::model()->findAll();		


		$this->render('registration',array('model'=>$model, 'countries'=>$countries, 'user'=>$user));

<?php $form=$this->beginWidget('CActiveForm', array(



));  foreach($countries as $country) ?>

	<div class="frmFields">

		<?php echo $form->labelEx($model,'title'); ?>

		<?php echo $form->dropDownList($model,'title', $user); ?>

		<?php echo $form->error($model,'title'); ?>


	<div class="frmFields">

		<?php echo $form->labelEx($model,'nationality'); ?>

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

		<?php echo $form->error($model,'nationality'); ?>


I’m getting empty data in the first dropdownlist and getting 2 values in the country dropdown. First value is Zimbabwe, the second is it’s dialing code 127. I just want to get the country field values from the table.

If I use <?php echo $form->dropDownList($model,‘nationality’, $country->country); ?> I get error Invalid argument supplied for foreach(). Undefined variable: country

If I remove foreach and use $countries->country I get error: Trying to get property of non-object

If I use $countries[‘country’] I get error: Undefined index: country

Have you checked dropDownList syntax?

You need a "classic" array, while findAll() returns an array of active records.

You may (should) use listData() to extract the arrays with relevant value=>label pairs from your $countries and $user arrays.

I was able to solve it with this


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

For the $user case I have title and gender fields having enum data types in userregistration table. The table is empty now. How can I get those enum values in dropDownList ?

See here: http://www.yiiframework.com/forum/index.php/topic/10079-enum-db-type-in-yii/