2017-05-03 90 views
0

我在这里有一个问题。我设置了itemsale表之间的关系,现在我的GridViewItem name显示为id's。但我需要的是它将显示Item names而不是ID's。我应该怎么做?如何在GridView Yii2中显示名称而不是id?

这里是我的GridView column

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

我在想,我应该写一个函数if声明,但我有很多的名字,这将是很长的。有没有更简单的方法来解决它?

回答

1

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

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

哦天啊,那太简单了..谢谢你,gmc! – HELPME

0

在我的情况下,运营商表和产品表。 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']); 
    } 
相关问题