3

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.

1
  • post full code of gridview Commented Dec 24, 2015 at 12:23

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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',
              ],
         ]); 
?>

24 Comments

Actually I have pass data through the dataProvider. I am not added above this dataProvider function ......I added this grid view change but yet not working
I have update the answer using Member for retrieve the username .
Error:Trying to get property of non-object
At this line got error:return Member::find()->where(['id' =>$model->memberID])->one()->firtsName;
I have update the answer ... a mistake value instead of 'value'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.