Model Block.php has relation defined in the following way
public function relations()
{
return array(
'district' => array(self::BELONGS_TO, 'District', 'd_id'),
);
}
Now I can make the dropDownList in the following way.
$data=CHtml::listData(Block::model()->findAll(),'id','name','district.name');//district.name is added as fourth parameter
echo CHtml::activeDropDownList($model,'name',$data);
ALTER TABLE `index_region`
ADD FOREIGN KEY (`country`) REFERENCES `decanvwp_main`.`index_country` (`id`)
ON DELETE NO ACTION ON UPDATE NO ACTION ;
and generated a new model (Region, using the index_region table) and that had the following relations:
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'country0' => array(self::BELONGS_TO, 'IndexCountry', 'Country'),
);
}
and i changed that to:
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'country' => array(self::BELONGS_TO, 'Country', 'id'),
);
}
Now it does list all the regions, but not optgroup’s with the country names…
$model=new Region;
$data=CHtml::listData(Region::model()->findAll(),'id','name',function($model){ //passing the anonymous function as a parameter.
return Country::model()->findByPk($model->country)->name;
});
echo CHtml::activeDropDownList($model,'name',$data);
explode() expects parameter 2 to be string, object given
on your last suggestion.
But thanks for your effort anyway, seenivasan
And the first suggestion (i got the relations to work properly) just feeds in one country and one region after eachother till there is no more countrys left, and it just prints the rest of the regions in the last country(optgroup). And non of the regions belong to the country-optgroup they are in.
Anyone else care to help me with a suggestion? Im thinking i have to sort the data before making a list, like foreach Country, list regions…
CDbException
CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'name' in order clause is ambiguous. The SQL statement executed was: SELECT `t`.`id` AS `t0_c0`, `t`.`name` AS `t0_c1`, `t`.`flag` AS `t0_c2`, `Regions`.`id` AS `t1_c0`, `Regions`.`country` AS `t1_c1`, `Regions`.`name` AS `t1_c2` FROM `index_country` `t` LEFT OUTER JOIN `index_region` `Regions` ON (`Regions`.`country`=`t`.`id`) ORDER BY name
I feel like i should be able to troubleshoot alittle on my own, but i cant make any sense of this message…
SELECT `t`.`id` AS `t0_c0`, `t`.`name` AS `t0_c1`, `r`.`id` AS `t1_c0`, `r`.`countryId` AS `t1_c1`, `r`.`name` AS `t1_c2`
FROM `a_country` `t`
LEFT OUTER JOIN `a_region` `r` ON (`r`.`countryId`=`t`.`id`)
ORDER BY t.name, r.name