simba
(Tiangooden)
October 10, 2012, 1:30am
1
does anybody know how to populate a dropdown list with a mongodb collection?
$list = CHtml::listData($industryModels, '_id', 'name');
echo $form->dropDownListRow($model, 'industry_id', $list);'
wont work because
_id
is a mongoId object and toString is not automatically called. i get the following error:
PHP warning Illegal offset type…which based on the stack-trace means an object cannot be used as an array key.
so how do i get the mongoId string to use as the key in the dropdown list???
redguy
(Maciej Lizewski)
October 10, 2012, 6:10am
2
you could add virtual attribute to your model:
function getStringId() {
return $this->_id->toString();
}
and use it in CHtml::listData($industryModels, ‘stringId’, ‘name’);
redguy solved half of my problem - the other half is sometimes the dropdown will not select the correct item if the model’s attribute is not stringified. So something like this helps out:
$model->industry_id = (string) $model->industry_id;
echo $form->dropDownListRow($model, 'industry_id', $list);
Or you just set up your model’s afterFind() method to stringify the IDs, and the beforeSave() method to re-MongoIdify them.