doulidai6316 2015-12-24 12:17
浏览 103
已采纳

如何在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.

  • 写回答

2条回答 默认 最新

  • douwulu2576 2015-12-25 02:17
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 vscode platformio
  • ¥15 代写uni代码,app唤醒
  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办