Dropdown box with ENUM in Yii
I have a table in my database, one of its data fields - ENUM
now I am working in a Yii framework and I want to create a dropdown and the list we want to use are the lists that are contained in ENUM
.
Example:
table x
field -> category -> ENUM(HARD,MEDIUM,EASY)
I want to create a dropdown and an option HARD
, MEDIUM
andEASY
+3
source to share
2 answers
make a function in your model to return an array of your list:
public function getOptions()
{
return array(
'EASY',
'MEDIUM',
'HARD',
);
}
then you can use it like this:
echo $form->dropdownList($model , 'category' , $model->options); // this will use that function to get the array
+5
source to share
You can also use parameters for these in Yii.
Example: Define categoryValues ββunder params array in config file.
'params' => array(
'categoryValues'=>array(
'EASY',
'MEDIUM',
'HARD'
),
),
Using:
echo $form->dropdownList($model , 'category' , Yii::app()->params['categoryValues']);
0
source to share