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);
}
}
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.
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().
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.
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’)
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.
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_infot LEFT OUTER JOIN vsms_sector_industry_masterindustryid 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.