Widget With Dropdownlist Populated From Database

Hi

I’ve made a widget which has a drop down list that I’d like to populate from a values in a table. Using By Example: CHtml (example 4), I’ve got this far:


class SidebarPropertySearch extends CWidget

{

	public function run() {

        //retrieve the models from the db

        $models = Region::model()->findAll();

	//format model as $key=>$value with listData

	$list = CHtml::listData($models, 'id', 'region');  /id and region are the two columns in the table


        $this->render('sidebarPropertySearchWidget', array(

            'models'=>$models   

        ));

    }

}

The problem is I can’t work out what I need to put in the widget view file to render the dropdownbox. Any help would be much appreciated.

Thanks

In your case, it’s probably easier just to output the drop down box directly in the widget:




class SidebarPropertySearch extends CWidget

{

    public function run()

    {

        //retrieve the models from the db

        $models = Region::model()->findAll();

        //format model as $key=>$value with listData

        $list = CHtml::listData($models, 'id', 'region');  //id and region are the two columns in the table


        echo CHtml::dropDownList(/*...*/); // Or CHtml::activeDropDownList() etc.

    }

}



Thanks for the reply, there’s alot of other stuff in the widget as well though. It’s also a form, should I be using CHtml::dropDwonList or $form->dropDownList?

Either way it give me an error:

Also the Region model is in a module called admin, so should I have something like:


$models = application.modules.admin.Region::model()

?

OK so this seems to work on the widget view file:


echo CHtml::dropDownList('PropertyType', '', CHtml::listData($models, 'id', 'region'), array('empty' => 'Select a region'))

;