Dropdownlist

I came across the following problem.

I intended to use the CActiveForm.dropDownList() method

in a form where I’m collecting user data.


echo $form->dropDownList($myModel,'SearchDeadline',$some_list);

As stated in the reference guide , the first parameter of dropDownList()

should be the data model.

Beeing unaware , I used something like this :


$myModel=MyModel::model()->findAll('ID=:ID',array(':ID'=>'55'));

Unfortunatelly , I got the error : get_class() expects parameter 1 to be object, array given

OK , I see , findAll() , returns an array and dropDownList is expecting a CModel class.

How else could I retrieve data from the model ?

Is there an alternative to findAll , which returns a Cmodel class ?

I’m stuck. :unsure:

if ur $model inside $form->dropdownlist() is only for , to mention attribute then why not u do like this





echo $form->dropDownList(new ModelName,'SearchDeadline',$some_list);






Yes, it’s called find(), or findByPk() if ID is your primary key. findAll() returns an array of records.

find() returns a single record only…

Not very useful for a dropdown list ;D

Try this:




$myModel = new MyModel();

$myModel->id = 55;

echo $form->dropDownList($myModel,'SearchDeadline',$some_list);



Then you don’t understand how dropDownList() works. $myModel->SearchDeadline is the property of the single model that you’re saving, and $some_list is the list of values for the dropdown. All of this is explained in the Working with Forms guide.

I apologize , you are absolutely right.

From tutorial:

  1. First, you get data from the database:

$models = categories::model()->findAll(

                 array('order' => 'category_name'));

  1. Then you format the data with a "listData()" function:

$list = CHtml::listData($models, 

                'category_id', 'category_name');

  1. Now you can use $list to fill your <select> tag with options:

<?php echo CHtml::dropDownList('categories', $category, 

              $list,

              array('empty' => '(Select a category'));

Yes , but CHtml::dropDownList has different parameters then CActiveForm::dropDownList

CHtml


public static string dropDownList(string $name, string $select, array $data, array $htmlOptions=array ( ))

CActiveForm


public string dropDownList(CModel $model, string $attribute, array $data, array $htmlOptions=array ( ))

??

Are you still stuck?

Check the differences of the following 3 groups in the reference.

[list=1]

[*]CHtml::textField, CHtml::dropDownList, …

[*]CHtml::activeTextField, CHtml::activeDropDownList, …

[*]CForm::textField, CActiveForm::dropDownList, …

[/list]

The 1st one uses ‘name’ and ‘value’ to create the input. We don’t use data model here.

The 2nd one uses a data model (‘model’ and ‘attribute’). ‘name’ and ‘value’ of the input are created from it.

The last one is meant for active form, and is a wrapper of the 2nd one.

No , thanks for asking.

I used the CHtml ‘version’ of dropDownlist , gave the required field a specific name via htmloptions array so I’ll be able to get the value via POST when submitting the form.