[Solved]Dependent Dropdown List

I am trying to create a dependent dropdown box and i checked all the forum topics related to dropdown. but i am not able to get the output at all… that’s why i am sending you this message. pls help me

my controller file


<?php


class VsmsEmpInfoController extends Controller

{

        

     


      public function actionDynamicSkill()

    {    

        $industryid = $_POST['VsmsEmpInfo']['industryid'];

            $data=VsmsEmpInfo::model()->findAll('industryid=:industryid',

                    array(':industryid'=> $industryid));


            $data=CHtml::listData($data,'skillid','skillname');

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

               echo CHtml::tag('option',

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

            }

        }




       

my view file register.php


<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

        'id'=>'vsms-emp-info-register-form',

         'enableAjaxValidation'=>true,

        

)); ?>


        <p class="note">Fields with <span class="required">*</span> are required.</p>


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

    

    


    <div class="row">

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

        

                <?php echo $form->dropdownlist($model,'industryid',CHtml::listData(VsmsIndustryMaster::model()->findAll(), 'industrytypeid', 'industrytypedesc'),                                  array(

                                'prompt'=>'Select a Sector',

                                'ajax'=> array( 

                                'type' => 'POST',

                                'url'=>CController::createUrl('VsmsEmpInfo/DynamicSkill'),

                                'replace'=>'#skillid', 

                                )    

                        ) 

                );

                 ?>

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

        </div>

    

        <div class="row">

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

                <?php echo $form->dropdownlist($model,'skillid',array()); ?>

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

        </div>


    

        


        <div class="row buttons">

                <?php echo CHtml::submitButton('Submit'); ?>

        </div>


<?php $this->endWidget(); ?>


</div><!-- form -->

Waiting for your reply

Do you see the ajax call with firebug?

check this answer I made to similar thread - http://www.yiiframework.com/forum/index.php?/topic/20511-ddd-2-models-on-a-3rd-models-form/page__view__findpost__p__100614

get this errors in firebug

<h1>CException</h1>

<p>Property "VsmsEmpInfo.skillname" is not defined. (C:\wamp\www\yii\framework\base\CComponent.php:131)</p><pre>

Should i do something in the public function relations()

If "skillname" is not a property of VsmsEmpInfo, i.e. VsmsEmpInfo only has "skillid" field and you have to look up another table for "skillname", then your VsmsEmpInfoController::actionDynamicSkill() have to be changed according to the relation.

so should i add a relation inside public function relation()

like this

‘skillid’ =>array(self::BELONGS_TO, ‘VsmsSkillMaster’, ‘skillid’)

where vsms_skill_master is the table i hav to refer to get the skillname

i have created model file for vsms_skill_master

No, you already have an attribute named “skillid” in your VsmsEmpInfo model, don’t you?

It should be:




'skill' =>array(self::BELONGS_TO, 'VsmsSkillMaster', 'skillid')



But provided that you have setup the foreign key constraint correctly, you should already have that relation declared if you have let Gii generate your model.

Anyway, using the relation, VsmsEmpInfoController::actionDynamicSkill() can be …




    public function actionDynamicSkill()

    {    

        $industryid = $_POST['VsmsEmpInfo']['industryid'];

        $data=VsmsEmpInfo::model()->with('skill')->findAll('industryid=:industryid',

            array(':industryid'=> $industryid));


        $data=CHtml::listData($data,'t.skillid','skill.skillname');

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

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

        }

    }



[EDIT]

[s]Sorry, I’m not sure about the parameters for CHtml::listData().

Could be




        $data=CHtml::listData($data,'skillid','skill->skillname');



?

[/s]

[EDIT2] ;D

model()->with(‘skill’)

What do u mean by skill? Sorry i din understand that.

i simple tried and got the error

<h1>CDbException</h1>

<p>Relation “skill” is not defined in active record class “VsmsEmpInfo”. (C:\wamp\www\yii\framework\db\ar\CActiveFinder.php:214)</p><pre>#0 C:\wamp\www\yii\framework\db\ar\CActiveFinder.php(306): CActiveFinder->buildJoinTree(Object(CJoinElement), ‘skill’)

As the error says, I meant "skill" as a relation name. You may choose any name for it, e.g. "vsms_skill" or something like that. But "skillid" is not good, because you already have a "skillid" field in VsmsEmpInfo.

Really sorry what relation should i use??

when i used "vsms_skill"

Relation “vsms_skill” is not defined in active record class “VsmsEmpInfo”. (C:\wamp\www\yii\framework\db\ar\CActiveFinder.php:214)</p><pre>#0 C:\wamp\www\yii\framework\db\ar\CActiveFinder.php(306): CActiveFinder->buildJoinTree(Object(CJoinElement), ‘vsms_skill’)

Ah, you have to define the relation.

If you do not know what you should do, I recommend you to read through the Relational Active Record section of the guide. It’s a MUST.

http://www.yiiframework.com/doc/guide/1.1/en/database.arr

It’s not like that… details from the second table can be taken only by referring to industryid… so i am checking that relation to get the skill names… but not working … giving a error that industryid cant be used in where clause because it’s ambiguous. that is industry id is present in both tables. is it because of that?

Integrity constraint violation: 1052 Column ‘industryid’ in where clause is ambiguous.

It means that both VsmsEmpInfo and VsmsSkillMaster have an ‘industryid’ field.

Then you have to use ‘t.industryid’ for VsmsEmpInfo::industryid or ‘your_relation_name.industryid’ for VsmsSkillMaster::industryid.

But if VsmsSkillMaster has ‘industryid’, why don’t you directly refer to the VsmsSkillMaster ?




    public function actionDynamicSkill()

    {    

        $industryid = $_POST['VsmsEmpInfo']['industryid'];

        $data=VsmsMasterSkill::model()->findAll('industryid=:industryid',

            array(':industryid'=> $industryid));


        $data=CHtml::listData($data,'skillid','skillname');

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

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

        }

    }



I just check out your code, some1 may have already said that but the field skillid has no id and it should have, as you are using it


<?php echo $form->dropdownlist($model,'skillid',array('id'=>'skillid')); ?>

also you are using replace, that will replace the whole dropdown code, change it to update, since your controller are only outputting the options


'update'=>'#skillid', 

$data=VsmsMasterSkill::model()

I have tried that earlier itself but again the Integrity constraint violation: 1052 Column ‘industryid’ in where clause is ambiguous.

Even after trying this my sql query error is

CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1052 Column ‘industryid’ in where clause is ambiguous.

The SQL statement executed was: SELECT t.empid AS t0_c0, t.userid AS t0_c1, t.empcode AS t0_c2, t.name AS t0_c3, t.companyname AS t0_c4, t.sectorid AS t0_c5, t.industryid AS t0_c6, t.otherindustry AS t0_c7, t.skillid AS t0_c8

FROM vsms_emp_info t LEFT OUTER JOIN vsms_sector_industry_master industryid ON (t.industryid=industryid.``) WHERE (industryid=:industryid)

You should not give the same name to a relation that is already used as a column name.

Your VsmsEmpInfo has a property( =attribute, =column) named “industryid”, and at the same time it has a relation named “industryid”. That’s why the wrong SQL is constructed by Yii AR.

Thank you…

everything started working as soon i corrected the relation function.

thanx a lot to all…