如何在yii2中以网格视图显示另一个表的数据?

如何在yii2中以网格视图显示另一个表的数据?

问题描述:

In siteController I write query and pass array of dataProvider to index.php to display contain in table form. In index.php I want to display member name instead of memberID. For that I have write inner query and which is successfully run on command prompt successfully.Here I am not able to print first name instead of 'member id'

      public function actionIndex()
      {              
     $query = new \yii\db\Query;
      $query->select(['member.firstName',
                    'complaint.practiceCode','complaint.id',
                    'complaint.description','member.firstName'])
           ->from(['complaint'])
           ->innerJoin(['member','complaint.memberID = member.id'])
           ->groupBy(['complaint.id'])
           ->where(['complaint.deleted' => 'N']);
     $query->createCommand();

Here I have pass data by creating $dataProvider4 but I am not able to set value of firstName instead of memberID.

      $dataProvider4= new ActiveDataProvider([
            'query' => $query,
            'pagination' => false,
       ]);

        return $this->render('index', [
       'dataProvider4'=>$dataProvider4]);

<?= GridView::widget([
    'dataProvider'=>$dataProvider4,
    'summary'=>'Total'.'&nbsp<b>'.$complaintModel.'</b>&nbsp'.'Complaints',
    'columns' => [
       ['class' => 'yii\grid\SerialColumn'],
           'practiceCode',
            // 'memberID',
            'description',
            'status',
       ],
    ]); ?>

I have pass data through the dataProvider.

siteController code>中我编写查询并传递 dataProvider code>的数组 以 index.php code>显示包含在表格中。 在 index.php code>中,我想显示成员名称 code>而不是 memberID code>。 为此,我编写了内部查询,并成功在命令提示符下成功运行。这里我无法打印 first name code>而不是' member id code>' p>

  public function actionIndex()
 {
 $ query = new \ yii \ db \ Query; 
 $ query-&gt; select(['member.firstName',
'  complaint.practiceCode','complaint.id',
'injection.description','member.firstName'])
  - &gt; from(['complaint'])
  - &gt; innerJoin(['member'  ,'complaint.memberID = member.id'])
  - &gt; groupBy(['complaint.id'])
  - &gt; where(['complaint.deleted'=&gt;'N']); \  n $ query-&gt; createCommand(); 
  code>  pre> 
 
 

这里我通过创建 $ dataProvider4 code>传递数据,但我无法设置 值 firstName code>而不是 memberID code>。 p>

  $ dataProvider4 = new ActiveDataProvider([
'query'=&gt;  $查询,
  'pagination'=&gt;  false,
]); 
 
返回$ this-&gt; render('index',[
'dataProvider4'=&gt; $ dataProvider4]); 
 
&lt;?= GridView :: widget([  
'dataProvider'=&gt; $ dataProvider4,
'summary'=&gt;'总计'。'&amp; nbsp&lt; b&gt;'。$ complaintModel。'&lt; / b&gt;&amp; nbsp'。'投诉',  
'列'=&gt; [
 ['class'=&gt;'yii \ grid \ SerialColumn'],
'ex练习代码',
 //'memberID',
'description',
'  status',
],
]);  ?&gt; 
  code>  pre> 
 
 

我通过dataProvider传递数据。 p> div>

Since you are using a powerful framework, then you'd be better to let the framework do the complicated thing for you, rather than trying to write your own queries. It's what Yii was designed for. Try this in your action.

public function actionIndex()
    {              
    $query = Member::find()->
        ->select(['firstName', complaint.practiceCode', complaint.id', 'complaint.description'])
        ->groupBy(['complaint.id'])
        ->joinWith('complaints')//Tells Yii to use the complains relation that we define below. By default it is an inner join
        ->where(['complaint.deleted' => 'N']);

$dataProvider= new ActiveDataProvider([
    'query' => $query,
    'pagination' => false,
]);

return $this->render('index', [
    'dataProvider4'=>$dataProvider]);

In your model you will need to define a relation that you can use in the query;

public function getComplaints(){
    return $this->hasMany(Complaints::className(), 'memberID' => 'id');
}

This is useful as it will allow you to get complaints without having to write your own query to get them.

Yii will sort out all the column names for you, and write the query.

The query is not related to dataProvider and then is unuseful

If you want extend the query you should do this and assign to the query used in dataprovider not only perform a query. Anyway you can get the name of the member using an anonymous function retrieve the value;

<?= GridView::widget([
        'dataProvider' => $dataProvider4,
        //'summary'=>'Total'.'&nbsp<b>'.$complaintModel.'</b>&nbsp'.'Complaints',
        'columns' => [
           ['class' => 'yii\grid\SerialColumn'],
           'practiceCode',
          /* ['attribute' => '',
            'value' => function ($model) {
               $myMemberModel =  Member::find()->
                       where(['id'=> $model->memberID ])->one();
               if (isset($myMemberModel) {
                  return $myMemberModel->firtsName;
               } else {
                 return ' id : '.  $model->memberID . ' do not match ';
               }
             }],*/
              'firstName',
               'memberID',
              'description',
              'status',
              ],
         ]); 
?>