Dynamic Dropdown On Many To Many Tables

I have working on create dynamic dropdowns (many to many relation) code. My problem is that On Update 2nd and 3rd dynamic dropdown does not show saved values/ I cannot update.

Base structure/classes had been created with gii.

Relations are following:

Table ticket has fields for sector, company, person. Sector has many to many relation with Company, and company has many to many relation with person.

for design of relations look @ attached file.

_form.php




<div class="row">

          <?php echo $form->dropDownList($model, 'ticket_sector', CHtml::listData(Sector::model()->findAll(), 'idsector', 'sector_name'),array(

                'empty'=>'Select sector',

                'ajax' => array('type'=>'POST',

                'url'=>$this->createUrl('ticket/dynamiccompanies'), //url to call.

                'update'=>'#'.CHtml::activeId($model, 'ticket_requestor_company'), //selector to update

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

                ))); ?>

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

        </div>

        

        <div class="row">

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

                <?php 

                echo $form->dropDownList($model, 'ticket_requestor_company',

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

                        array(), array(

                   'empty'=>'Select sector',

                    'ajax' => array('type'=>'POST',

                'url'=>$this->createUrl('ticket/dynamicemployees'), //url to call.

                'update'=>'#'.CHtml::activeId($model, 'ticket_requestor'), //selector to update

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

                //'success'=> 'function(data) { $("ticket_requestor").empty(); $("ticket_requestor").append(data); } ',

                )));

                ?>

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

	</div>


        <div class="row">

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

		<?php echo $form->dropDownList($model,'ticket_requestor',array(), array('prompt'=>'Select company')); ?>

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

	</div>



My controller => TicketController.php




       public function actionDynamiccompanies()

        {

                        $data= Sector::model()->findByPk((int) $_POST['ticket_sector']);

                        $data= $data->companies;

                        $dataArray = CHtml::listData($data, 'idcompany', 'company_name');

                        echo CHtml::tag('option', array('value'=>'0'),CHtml::encode("Select company"),true);

                        foreach ($dataArray as $value=>$company_name)

                        {

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

                        }

        }

        

              public function actionDynamicemployees()

        {

                        $pers= Company::model()->findByPk((int) $_POST['ticket_requestor_company']);

                        $data = $pers->people;

                        $dataArray = CHtml::listData($data, 'idperson', 'person_surname');

                        echo CHtml::tag('option', array('value'=>'0'),CHtml::encode("Select employee"),true);

                        foreach ($dataArray as $value=>$person_surname)

                        {

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

                        }

}



Any suggestions are much appreciated.

Thanks in advance for your help.

[font=“Arial”]Hi, I’m not sure but maybe the problem is this part:[/font]


'js:this.value'

that should be rewritten to: [font="Arial"]


'js:$(this).val()'

Also consider using Firebug to check if the ajax request is sending the data to the controller in the correct format.

[/font]

Thanks for your reply faridplus.

[indent]Both versions (yours and mine) of the code work when creating a new record. Problem is that on update dropdowns are not repopulated (firebug on create shows that sended data are OK, on update nothing happens as the 1st dropDown fires ajax only on change, so no POSTing).I suspect that culprit is the empty array in $form->dropDownList(…, array(), … => could be replaced with something like array($model->ticket_requestor_company), but this way I get id, not realy the "user friendly descriptive" name…[/indent]

[indent]I suppose that solution could be a function that returns idcompany=>company_name…

Unfortunatelly I do not know what "format" empty array accepts…[/indent]

I have solved the problem with the following code (despite it does not seem to be "elegant enough"):

As mentioned before modifying empty array is the solution,

array()

should be replaced with

array($model->ticket_requestor_company=>$model->getCompanyName($model->ticket_requestor_company))

Unfortunatelly this breaks the code for create …, while update works. Therefor I have been forced to use if fork in _form.php.

If anyone has a suggestion how to make this code more ‘slim’ I would appreciate it very much. Thanks.

Hope this helps someone with the same problem.

My _form.php is now:




	<?php echo $form->errorSummary($model); ?>


                <div class="row">

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

		<?php 

                echo $form->dropDownList($model, 'ticket_sector', CHtml::listData(Sector::model()->findAll(), 'idsector', 'sector_name'),array(

                'empty'=>'Select sector',

                'ajax' => array('type'=>'POST',

                'url'=>$this->createUrl('ticket/dynamiccompanies'), //url to call.

                'update'=>'#'.CHtml::activeId($model, 'ticket_requestor_company'), //selector to update

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

                ))); ?>

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

        </div>

        

        <div class="row">

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

                <?php 

                if ($model->ticket_requestor_company === NULL) {

                    echo $form->dropDownList($model, 'ticket_requestor_company',

                        array(),  array(

                'empty'=>'Select Sector first',

                            'ajax' => array('type'=>'POST',

                'url'=>$this->createUrl('ticket/dynamicemployees'), //url to call.

                'update'=>'#'.CHtml::activeId($model, 'ticket_requestor'), //selector to update

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

                )));}

                else {

                echo $form->dropDownList($model, 'ticket_requestor_company',

                        array($model->ticket_requestor_company=>$model->getCompanyName($model->ticket_requestor_company)),  array(

                'empty'=>'Select Sector first',

                            'ajax' => array('type'=>'POST',

                'url'=>$this->createUrl('ticket/dynamicemployees'), //url to call.

                'update'=>'#'.CHtml::activeId($model, 'ticket_requestor'), //selector to update

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

                

                )));} ?>

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

	</div>


        <div class="row">

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

		<?php 

                if ($model->ticket_requestor === NULL) {

                    echo $form->dropDownList($model,'ticket_requestor', array(),

                    array('prompt'=>'Select company'));                    

                } else {

                    echo $form->dropDownList($model,'ticket_requestor', array(

                    $model->ticket_requestor=>$model->getPersonName($model->ticket_requestor)),

                    array('prompt'=>'Select company'));

                }

                ?>

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

	</div>



while TicketController.php is unchanged




        public function actionDynamiccompanies()

        {

                        $data= Sector::model()->findByPk((int) $_POST['ticket_sector']);

                        $data= $data->companies;

                        $dataArray = CHtml::listData($data, 'idcompany', 'company_name');

                        echo CHtml::tag('option', array('value'=>'0'),CHtml::encode("Select company"),true);

                        foreach ($dataArray as $value=>$company_name)

                        {

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

                        }

        }

        

              public function actionDynamicemployees()

        {

                        $pers= Company::model()->findByPk((int) $_POST['ticket_requestor_company']);

                        $data = $pers->people;

                        $dataArray = CHtml::listData($data, 'idperson', 'person_surname');

                       echo CHtml::tag('option', array('value'=>'0'),CHtml::encode("Select employee"),true);

                        foreach ($dataArray as $value=>$person_surname)

                        {

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

                        }

        }



I have been forced to create in relative models (Ticket, Person) functions to retrieve names:

Ticket.php




       public function getCompanyName($id)

       {

           $nameObject = Company::model()->findByPk($id);

           return $nameObject->company_name;

       }

       

       public function getPersonName($id)

       {

           $nameObject = Person::model()->findByPk($id);

           return $nameObject->getFullName();

       }



Person.php




        public function getFullName()

        {

            return $this->person_name . ' '. $this->person_surname;

        }