Problem with ActiveDropDownList in a form

Just learning yii and creating a simple application… ran into a problem with activedropdown.

I have 2 tables…

User

====

  • user_id (integer, primary key)

  • user_name (varchar)

  • user_country (integer)

Country

=======

  • country_id (integer, primary key)

  • country_name (varchar)

in the country model, i’m have a function that gets all country names from the database with their ids.




public function getCountryList()

        {

            $criteria=new CDbCriteria;

            $criteria->select=country_id,country_name';

            $qCountries=Country::model()->findAll($criteria);

        }



in the _form.php belonging to user view, i have the following code, but the drop down box does not even appear. I have a blank space there…




<?php CHtml::activeDropDownList($model, 'user_country', CHtml::listData($model,'country_id','country_name')); ?>



User model contains the following relation…




'usr_country'=>array(self::HAS_ONE, 'Country', 'country_id'),



Database type is InnoDB. There’s no foreign key in the user table that links user_country field to country_id field in Country table.

Could someone please point me to my mistake?

Hi, menatep.

In your _form.php just put the following code for dropdown list:


<?php echo CHtml::activeDropDownList($model, 'user_country', CHtml::listData(Country::model()->findAll(),'country_id','country_name')); ?>

Note that in your code you use the User model both in ‘activeDropDownList’ and ‘listData’ - that was mistake. And a little typo - you forgot to print the function result.

P.S. Why not to use the foregn keys?

Wow that works… thanks a lot! and i was making things more complicated. That one line works, so i guess i’ll remove all the other code.

I use foreign keys but i don’t want to use too many of them to maintain high performance.