How do I add a value to my drop down list?


I am returning a set of records for list_data, but want to add a default value not stored in the table.

$id = empty($id) ? 0 : $id;

$results = self::model()->findAllBySql('select id, menu_title from sales where id <> :p1 order by menu_title', array(':p1' => $id));

$results = array(0=>'No Parent') + $results; // not working

return $results;

Thank you

First of all, please take a notice that findAllBySql() method will return array of objects

Second, to get list of values it’s usually convenient to use listData() method.


$data = CHtml::listData(Sales::model()->findAll(), 'id', 'menu_title');

listData() docs

Third, usually I use dropDownList() or activeDropDownList() method to render drop-downs, and it allows to add your ‘default’ value.


echo CHtml::dropDownList('MyName', false, $data, array('empty'=>array(0=>'NoParent')));

dropDownList() docs

and check out activeDropDownList() docs as well.

Hope it’ll help.

Thank you…