I have been trying to work through the following scenario to no avail. Would appreciate if someone could help me out with this.
Given the db:
In my CGridView for City I would like to also display Name from Country.
The farthest that I got was being able to display a dropdown list of the Country Names (so the filter works) but I could not get the Country Names to populate the fields in CGridView as that column kept referencing the City Model.
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'city-grid',
'dataProvider' => $model->search(),
'filter' => City::model(),
'columns' => array(
...
'CountryName'=>array(
'name'=>'Name',
'value'=>'$Country->Name',
'filter' => GxHtml::listDataEx(Country::model()->findAll(array('order'=>'Name')), 'Name', 'Name'),
),
Here is my current code minus all the attempts I have gone through to try and make this happen. This works properly to display City(CityId, Name) and Region(Name).
views/city/admin.php
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'city-grid',
'dataProvider' => $model->search(),
'filter' => City::model(),
'columns' => array(
'CityId',
'Name',
array(
'name'=>'RegionCode',
'value'=>'GxHtml::valueEx($data->regionCode)',
'filter'=>GxHtml::listDataEx(Region::model()->findAllAttributes(null, true)),
),
array(
'class' => 'CButtonColumn',
),
),
)); ?>
models/_base/BaseCity.php
public function relations() {
return array(
'regionCode' => array(self::BELONGS_TO, 'Region', 'RegionCode'),
);
}
public function attributeLabels() {
return array(
'CityId' => Yii::t('app', 'City Id'),
'RegionCode' => Yii::t('app', 'Region'),
'Name' => Yii::t('app', 'Name'),
);
}
public function search() {
$criteria = new CDbCriteria;
$criteria->compare('CityId', $this->CityId);
$criteria->compare('RegionCode', $this->RegionCode);
$criteria->compare('Name', $this->Name, true);
return new CActiveDataProvider(get_class($this), array(
'criteria' => $criteria,
));
}