I have a MANY_MANY relation that I try to represent using several dropdown lists. I can create the dropdown lists fine using JavaScript when creating an entry, but I have problem updating it.
I have tried the following code in my _form.php:
// Notice: 'unitEnums' is my MANY_MANY relation
echo $form->dropDownList($model, 'unitEnums', CHtml::listData(UnitEnum::model()->findAll(),'id', 'name'),
array('empty'=>'', 'id'=>'Parameters_unitEnums_0', 'name'=>'Parameters[unitEnums][]'));
Which not suprisingly only gives me one dropdown list.
My second try is a little more successful:
// Notice: 'unitEnums' is my MANY_MANY relation
foreach($model->unitEnums as $i => $relatedModel)
{
echo $form->dropDownList($relatedModel, 'unitEnums', CHtml::listData(UnitEnum::model()->findAll(), 'id', 'name')),
array('empty'=>'', 'id'=>"Parameters_unitEnums_$i", 'name'=>'Parameters[unitEnums][]');
}
This creates the correct number of dropdown lists, but there are several problems. The selections are wrong, the htmlOptions are are not applied and the text ‘Array’ is written between every dropdown list as seen in the attachment.
1776
How can I create and fill all the needed dropdown lists when updating a entry?
You have some parentheses issues I think. And you need to not use ‘unitEnums’ as the second parameter to your dropDownList call. I’ll assume (and I might be wrong) you want to use the ID of the related model as the value in the dropdown list.
Try:
// Notice: 'unitEnums' is my MANY_MANY relation
foreach($model->unitEnums as $i => $relatedModel)
{
echo $form->dropDownList($relatedModel, 'id',
CHtml::listData(get_class($relatedModel)::model()->findAll(), 'id', 'name'),
array('empty'=>'', 'id'=>"Parameters_unitEnums_$i",
'name'=>'Parameters[unitEnums][]')
);
}