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 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器