Displaying all database records in a dropdown in Yii

I will show you how to display all database records of a certain table in a dropdownlist in Yii.

Model Code:

class UserType extends CActiveRecord {
     public function GetUserType(){
	$command= $connection->createCommand("SELECT * FROM ".$this->tableName());
	$rows = $command->queryAll(); 
	return CHtml::listData($rows,'id', 'type');

View Code:

echo $form->dropDownList($model,'user_type_id',UserType::model()->GetUserType());

The CHTML::listData() takes 3 parameters.
1st is the result of the row of your sql
2nd parameter is the value of your option. So it’s something like this <option value=”value here“>
3rd parameter is the value between <option>value here</option>