带有关系表的CGridView

时间:2013-12-01 21:07:38

标签: mysql yii yii-cactiverecord

我有两个关系表:location& location_types。

位置

  - location_id
  - location_name
  - location_type_id

Location_types

 - location_type_id
 - location_type

我使用CGridView小部件创建了一个视图,一切正常,甚至文件管理器字段都在工作,除了location_type列只显示location_type_id。

视图中的CGridView小部件:

$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
    array('name' => 'location', 'header' => 'Location'),
    array(           
        'name' => 'location_type_id', 
        'header' => 'Location Type'
        ),
),

Location_types模型的关系部分

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(
                'locationlocationtype_rel' => array(self::BELONGS_TO, 'Locations', 'location_type_id'),
    );
}

那么:我如何用location_type替换location_type_id?

1 个答案:

答案 0 :(得分:0)

 array(           
        'name' => 'location_type_id', 
        'header' => 'Location Type'
        'value' => '$data->locationlocationtype_rel->location_type' // use relations property here
        ),