Why do I need a Model to display a simple dropdownlist


I know that it is my lack of knowledge about Yii that is the problem, but IMHO is the documentation lacking examples or explanations :)

I had - and still have - a problem understanding how to use a array of data as datasource in CDetailView, but I gave up, and now I have the ‘same’ problem with a drop down list in a form, and to honest I’m very close to give up using Yii - I know it’s my lack of knowledge, so do NOT start to tell me that :)

Ok, let me try to explain:

In the documentation it is stated that you can use a associative array as data source, the problem is just that it seems the Yii have a different definition of a associative array


array(‘key1’ => ‘Value 1’, ‘key2’ => 'Value 2);

is a associative array (http://en.wikipedia.org/wiki/Associative_array), but it is not what Yii expect.

Now back to the subject.

I have a ‘simple’ form that asks to height, weight and activtilevel, I have created a model for the data and now in the view I would like to have a drop down list for the activity level

<div class="row">

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

		<?php echo $form->dropDownList($model,'activitylevel',$model->activitylevel, $data); ?>

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


Now the $data is my problem as I have tried 20 different combinations, but nothing seems to work:


$data [] = array(‘key’=> 0, ‘value’ => ‘select’);

$data [] = array(‘key’ => 1, ‘value’ => ‘Hard’);

$data [] = array(‘key’ => 2, ‘value’ => ‘Easy’);

So basically I need an example on how to build the array that Yii expect.

As I said, it is my lack of Yii knowledge, but I’m at the point of giving up on Yii due this ‘simple’ thing

Kind regards


This line has 1 argument too much:

<?php echo $form->dropDownList($model,'activitylevel',$model->activitylevel, $data); ?>

should be:

<?php echo $form->dropDownList($model,'activitylevel', $data); ?>

Note, that you use CActiveForm::dropDownList() here, which is different from CHtml::dropDownList().


i think the link above suppose can solve your problem… Add Oil.

Thanks both of you!