portishead
(Massivetdm850)
June 26, 2013, 2:33pm
1
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.
lalzada
(Lalzadamohmand)
June 26, 2013, 3:13pm
2
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);
Tsunami
(Erik)
June 26, 2013, 3:34pm
3
Yes, it’s called find(), or findByPk() if ID is your primary key. findAll() returns an array of records.
portishead
(Massivetdm850)
June 26, 2013, 8:23pm
4
find() returns a single record only…
Not very useful for a dropdown list
Try this:
$myModel = new MyModel();
$myModel->id = 55;
echo $form->dropDownList($myModel,'SearchDeadline',$some_list);
Tsunami
(Erik)
June 27, 2013, 6:27am
6
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.
portishead
(Massivetdm850)
June 27, 2013, 6:41am
7
I apologize , you are absolutely right.
Doojin
(Dmitry Papka)
June 27, 2013, 6:42am
8
From tutorial:
First, you get data from the database:
$models = categories::model()->findAll(
array('order' => 'category_name'));
Then you format the data with a "listData()" function:
$list = CHtml::listData($models,
'category_id', 'category_name');
Now you can use $list to fill your <select> tag with options:
<?php echo CHtml::dropDownList('categories', $category,
$list,
array('empty' => '(Select a category'));
portishead
(Massivetdm850)
June 27, 2013, 6:46am
9
Doojin:
From tutorial:
First, you get data from the database:
$models = categories::model()->findAll(
array('order' => 'category_name'));
Then you format the data with a "listData()" function:
$list = CHtml::listData($models,
'category_id', 'category_name');
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 ( ))
softark
(Softark)
June 27, 2013, 10:42am
10
??
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.
portishead
(Massivetdm850)
June 27, 2013, 12:19pm
11
softark:
??
Are you still stuck?
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.