dependent dropdown

Thank you very much for your amendment

I dont want to have any default or first entry to be selected. But i forgot to add "please select a value" as the first entry to the topmost dropdown. With this entry the behavior makes sense.

thanks for sharing your code example.

can you explain what is going on here in the controller?




// $model=$this->loadModel($id);

$model=Bill::model()->with('task', 'task.project', 'task.project.company')->findByPk((int)$id);



Bill::model - you are looking at the model for Bill, correct?

with( … is a method in that model? or in it’s parent?

what is ‘task.project.company’ is this a way of referencing objects and their relations to each other?

i need help understanding, thanks you

Yes, the model Bill should be loaded. To select the related entries in the project and company dropdown list the bill model needs to be extended by the project_id and company_id.




$model=Bill::model()->findByPk((int)$id);



Does only load the data related to the bill model itself. Each model extends CAtiveRecord. "with()" is a method of CActiveRecord and specifies which related objects should be eagerly loaded. More details can be found at http://www.yiiframework.com/doc/api/1.1/CActiveRecord

You’ve got that right!




$model=Bill::model()->with('task', 'task.project', 'task.project.company')->findByPk((int)$id);



Does load the model bill and the relation to task, project and company (company ist not realy required - the company id can be retrieved from project). The follwing code can be used to access the attributes of task and project




$model->project_id = $model->task->project_id;

$model->company_id = $model->task->project->company_id;



Thank you very much for your example Tom808! It’s great to have working examples to study!

I am brand new to frameworks and am trying to figure everything out.

Would you by any chance have any idea how to implement the following feature to your example :

To be able to add new Company, Project and Tasks should one want one that is not listed.

I’ve searched the forums, but haven’t found anything on the subject…

Kindest regards

Benn

Hi Benn,

this can’t be done by html - the select (dropdown) does not allow to enter any data.

You may use java script (search for "editable dropdown" or "editable select" http://www.google.de/#hl=en&q=editable+dropdown) or use jquery autocomplete http://jqueryui.com/demos/autocomplete/#combobox as basis for the required behavior. Most of these hacks use a standard select in the background and place a div or iframe with the input field in front of this.

Another (and maybe less complecate) approach would be to place a input element for new entries beside the select or to use a jquery dialog to enter new data.

Kind regards

Tom

Thanks. I had found that when I was developing my application without yii. I was just hoping there was an easy way to do it with yii.

I’ll look into integrating the jquery version.

Cheers

Benn

Hi Benn,

maybe someone has already written a yii extension for this. Please check http://www.yiiframework.com/extensions/?category=15.

jqautocomplete and jamselect could be interesting for you to start with.

Regards

Tom

Thanks, I’ll look into it and keep you posted (if anyone is interested)

[edit] : it seems the jui autocomplete plugin is already implemented as a widget in yii :

http://www.yiiframework.com/doc/api/1.1/CJuiAutoComplete :)

Ok, to anyone who might be interested, I’ve managed to get the CJuiAutoComplete working and populating the second dropdown list.

_form.php


	

<div class="row">		

  <?php echo $form->labelEx($model,'brand_id'); ?>		

  <?php $form->widget('zii.widgets.jui.CJuiAutoComplete', array(

            'model'=>$model,

            'attribute'=>'brand_id',

            'source'=>$this->createUrl('Ad/autocompleteBrand'),

            // additional javascript options for the autocomplete plugin

            'options'=>array('showAnim'=>'fold',),

            'htmlOptions'=>array('ajax' => array(

                      'type'=>'POST',

                      'url'=>CController::createUrl('Ad/getModelsOptions'),

                      'update'=>'#'.CHtml::activeId($model,'model_id'),

                      'beforeSend' => 'function(){

                          $("#page").addClass("loading");}',

                      'complete' => 'function(){

                          $("#page").removeClass("loading");

                          $("#' . CHtml::activeId($model,'model_id') . '").trigger("change");

                      }',

              ))

      ));?>		

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

</div>    

<div class="row">		

  <?php echo $form->labelEx($model,'model_id'); ?>		

  <?php echo $form->dropDownList($model,'model_id', CHtml::listData(Models::model()->findAll('brand_id= :brand', array(':brand'=>$model->brand_id)), 'id', 'name'), array(

        'ajax' => array(

  				'type'=>'POST',

  				'url'=>CController::createUrl('Ad/getVersionsOptions'),

  				'update'=>'#'.CHtml::activeId($model,'version_id'),

          'beforeSend' => 'function(){

              $("#page").addClass("loading");}',

          'complete' => 'function(){

              $("#page").removeClass("loading");}',

  			))); ?>		

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

</div>



AdController.php




    public function actionGetModelsOptions() {

        $brand = Brand::model()->findByAttributes(array("name"=>$_POST['Ad']['brand_id']));

        $data = Models::model()->findAll('brand_id=:brand_id',

                        array(':brand_id' => (int) $brand->id));

        $data = CHtml::listData($data, 'id', 'name');

        foreach ($data as $value => $name) {

            echo CHtml::tag('option',

                    array('value' => $value), CHtml::encode($name), true);

        }

    }


    public function actionAutocompleteBrand() {

	$res =array();


	if (isset($_GET['term'])) {

		// http://www.yiiframework.com/doc/guide/database.dao

		$qtxt ="SELECT name FROM tbl_brand WHERE name like :name";

		$command =Yii::app()->db->createCommand($qtxt);

		$command->bindValue(":name", '%'.$_GET['term'].'%', PDO::PARAM_STR);

		$res =$command->queryColumn();

	}


	echo CJSON::encode($res);

	Yii::app()->end();

    }



Now, anyone know how I can link a widget to the form builder in order to have it linked to the model attribute?

[edit]: made the necessary corrections in the code, it now behaves as it should (at least as far as rules are concerned :) )

Cheers

Wonderful!! thank you, it helped me a lot!! :)

This post is really helpful. Thanks :)

Thanks tom …code is really working for me. thanks again for sharing