简体   繁体   English

如何在gridview显示yii中显示两个表的结果

[英]how can i display the result of two tables in gridview display yii

I have two tables Table A and Table B. 表A和表B有两个表。

I have the pk of table A ,as fk called member_id in table B. 我有表A的pk,如表B中名为member_id的fk。

While girdview display of table B, I want to show the "Member name" in table A, using the "member_id" in table B. 在表B的girdview显示时,我想在表A中使用表B中的“member_id”显示“成员名称”。

$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}',
    ),
),
)); 

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}',
    ),
),
)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM