如何显示名称而不是id';在GridView Yii2中有什么?

如何显示名称而不是id';在GridView Yii2中有什么?,yii2,Yii2,我有个问题。我设置了item和sale表之间的关系,现在item name的GridView列显示了它的id。但我需要的是,它将显示项目名称,而不是ID的。我该怎么做 这是我的GridView列: [ 'attribute' => 'item_id', 'value' => ], 我想我应该用if语句编写一个函数,但是我有很多名字,而且会很长。有更简单的解决方法吗?假设您的关系名为getItems(),项目名称的字段名为name: [ 'attri

我有个问题。我设置了
item
sale
表之间的关系,现在
item name
GridView
列显示了它的
id
。但我需要的是,它将显示
项目名称
,而不是
ID的
。我该怎么做

这是我的
GridView列

[
     'attribute' => 'item_id',
      'value' => 
],

我想我应该用
if
语句编写一个函数,但是我有很多名字,而且会很长。有更简单的解决方法吗?

假设您的关系名为
getItems()
,项目名称的字段名为
name

[
     'attribute' => 'items.name'
],

在我的例子中,它是公司表和产品表。
comp_id
是公司表中的主键,它与产品表相关

<?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            'prod_id',
            'name',
            'description:ntext',
            [
                'attribute' => 'comp_id',
                'value'     => 'comp.name' //getComp()
            ],

            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?> 

//getcomp function the product model page.
 public function getComp()
    {`enter code here`
        return $this->hasOne(Company::className(), ['comp_id' => 'comp_id']);
    }

//getcomp函数显示产品型号页面。
公共函数getComp()
{`在这里输入代码`
返回$this->hasOne(Company::className(),['comp_id'=>'comp_id']);
}

哦,天哪,那太容易了。。谢谢你,gmc!