Reputation: 193
I have two tables Table A and Table B.
I have the pk of table A ,as fk called member_id in table B.
While girdview display of table B, I want to show the "Member name" in table A, using the "member_id" in table B.
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'transaction-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'member_id',
'Location',
...
array(
'class'=>'CButtonColumn',
'template'=>'{view}',
),
),
));
Upvotes: 1
Views: 3660
Reputation: 207828
You need to setup a relation to that table, and you can use the relation to reference that field like this:
$data->member->name;
In your transaction model you would place something like:
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(
'member' => array(self::BELONGS_TO, 'Member', 'member_id'),// foreign key
);
}
and for grid you would do like:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'transaction-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
array(
'header' => 'Member',
'name' => 'member_id',
'value' => '$data->member->name'
),
'Location',
...
array(
'class'=>'CButtonColumn',
'template'=>'{view}',
),
),
));
Upvotes: 4