Yii2:向gridview添加ID标签

时间:2017-11-19 08:44:53

标签: php gridview yii2-advanced-app

我在控制器中createCommand

  $receipts=new receipts();
     $query1 = new Query;
     $query1  ->select(['price','date','id']) 
         ->from('paymentreceipts')
         ->join(  'INNER JOIN','patient','patient.patient_id =paymentreceipts.patient_id' )
         ->where('patient.patient_id=:id', array(':id'=>$id))      
         ->orderby ('paymentreceipts.date');
     $command1 = $query1->createCommand();
     $dataProvider1 = $command1->queryAll();  
     $gridViewDataProvider3 = new \yii\data\ArrayDataProvider([
     'allModels' => $dataProvider1,
     'sort' => [
     'attributes' => ['price','reg_date','id'],
     ],
     ]);

  return $this->render('view', [
        'model' => $this->findModel($id),
         'gridViewDataProvider2' => $gridViewDataProvider2,
    ]);
在view.php中

  <?= 
    GridView::widget([
      'dataProvider' => $gridViewDataProvider2,
      //  'pjax'=>true,    
      'summary'=>'',
      'showFooter' => true,
      'columns' => [
          ['label' => 'رقم الخدمة',
          'attribute' => 'id',
          ],

          [
          //  'label'=>'url',
            'format' => 'raw',
            'value'=>function ($data) {
            return Html::a('$data->date','#',['class' => 'receipts','id'=>'id']);
          },
          ],           

          ]

])?&gt;

我需要的是:将'attribute' => 'id'号码中的ID添加到我在此处创建的链接['class' => 'receipts','id'=>'id']);

我使用'id'=> $gridViewDataProvider2->id,但它不起作用!

我的英语不好,所以也许我的问题不明确。

1 个答案:

答案 0 :(得分:0)

JOIN 语句是您没有收到“id”的原因。因为您的两个表都有“id”列,并且它变为不明确来读取它。更改您的查询,如下所示,

  $query1  ->select(['price','date','id as receiptId']) 
     ->from('paymentreceipts')
     ->join(  'INNER JOIN','patient','patient.patient_id =paymentreceipts.patient_id' )
     ->where('patient.patient_id=:id', array(':id'=>$id))      
     ->orderby ('paymentreceipts.date');
 $command1 = $query1->createCommand();

然后你可以像下面这样建立链接。

   return Html::a('$data->date','#',['class' => 'receipts','id'=>$data->receiptId]);