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 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探