Dependent Dropdown

im a newbie ive been trying to make a dependent dropdown following instructions from similar wikis but it doesnt seem to work . im also gettting this error: Object of class Maintenancerequest could not be converted to string

here is my form view(maintenancerequest)


<?php echo CHtml::dropDownList($model,'BUILDINGS_BUILDING_ID',

  CHtml::listData(Buildings::model()->findAll(),'BUILDING_ID','BUILDING_NAME'),

 

  array(

    'prompt'=>'Select building',

    'ajax' => array(

    'type'=>'POST', 

    'url'=>Yii::app()->createUrl('MaintenancerequestController/Loadrooms'), //or $this->createUrl('loadcities') if '$this' extends CController

    'update'=>'#roomname', //or 'success' => 'function(data){...handle the data in the way you want...}',

  'data'=>array('buildingid'=>'js:this.value'),


  )));	

echo CHtml::dropDownList('roomname','', array(), array('prompt'=>'Select room'));

  ?>	


		<?php echo $form->error($model,'BUILDINGS_BUILDING_ID'); ?>

my controller(MaintenancerequestController:


public function actionLoadrooms()

{

   $data=Rooms::model()->findAll('BUILDING_ID=:BUILDING_ID', 

   array(':BUILDING_ID'=>(int) $_POST['BUILDING_ID']));

 

   $data=CHtml::listData($data,'roomid','roomname');

 

   echo "<option value=''>Select room</option>";

   foreach($data as $value=>$roomname)

   echo CHtml::tag('option', array('value'=>$value),CHtml::encode($roomname),true);

}

rooms model :


public function relations()

	{

		

		return array(

			'Buildings' => array(self::BELONGS_TO, 'Buildings', 'BUIDING_ID'),

		);

	}

when creating a maintenace request . in the form user selects building , and has to specify room (room is dependent on building chosen)

kindly assist

Hi

You have so many syntax error in your coding

Following are




 'url'=>Yii::app()->createUrl('MaintenancerequestController/Loadrooms'),



you have to mention controller name without controller word like this [size=2]Yii::app()->createUrl(’[/size][size=2]maintenancerequest/loadrooms’)[/size]

[size=2]

[/size]

[size=2]


[/size]

echo CHtml::dropDownList('roomname','', array(), array('prompt'=>'Select room'));

[size=2]

[/size]

[size=2]

[/size]

[size=2]Where is the id in this dropdownlist…because you are updating with this ID [/size][size=2]roomname[/size]

Hi im following the example here http://www.yiiframework.com/wiki/429/an-easy-solution-for-dependent-dropdownlist-using-ajax/

but i have changed the controller name .

i dont get what you mean by id but i have changed to


  <?php echo CHtml::dropDownList($model,'BUILDINGS_BUILDING_ID',

  CHtml::listData(Buildings::model()->findAll(),'BUILDING_ID','BUILDING_NAME'),

 

  array(

    'prompt'=>'Select building',

    'ajax' => array(

    'type'=>'POST', 

    'url'=>Yii::app()->createUrl('Maintenancerequest/Loadrooms'), //or $this->createUrl('loadcities') if '$this' extends CController

    'update'=>'#roomid', //or 'success' => 'function(data){...handle the data in the way you want...}',

  'data'=>array('buildingid'=>'js:this.value'),


  )));	

echo CHtml::dropDownList('roomid','', array(), array('prompt'=>'Select room'));