简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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