[Yii]DropDown with model data

Riazul Karim Ivan

Normally when i generate any CRUD by gii in Yii, its just shows the text fields. If any one want to change it to drop down list, and want to fetch data from another table, then we have to do this:

<?php echo $form->dropDownList($model,’user_type_id’,
CHtml::listData(UserType::model()->findAll(), ‘id’, ‘type’),
array(’empty’=>’Select Type’));


$form->dropDownList is a widget.

UserType is a model.

CHtml::listData is a function which will genarate a select list from model data.

Here in listData function 2nd parameter is $valueField and 3rd parameter is $textField, which are equivalent to  HTMLselect options value and name.

View original post


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s