0

everyone this is my function that return the data provider to the gridview.

public function search($params)
    {



        $query = EventOrganizer::find()
        ->where(['event.approve' => 0])
        ->groupBy(['event.eventID']);

        // add conditions that should always apply here

        $query ->joinWith(['event', 'organizer']);
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

             $query->select(['event.eventID','event.title','event.startDate','event.startTime','event.endDate','event.endTime',
           'event.photo','event.description','event.location','event.approve',
           "GROUP_CONCAT(CONCAT(`organizer`.`name`,\" \", `organizer`.`organizerID`)) AS Name"])
            ->andFilterWhere(['like', 'event.eventID', $this->eventID])
            ->andFilterWhere(['like', 'organizer.organizerID', $this->organizerID]);


        return $dataProvider;

    }

The following is part of my code in gridview. When i trying to access data inside the data provider, I couldnt get to access the column which i had using group_concat.

 [
      'label'=> 'End Time',
      'attribute' => 'GROUP_CONCAT(CONCAT(`organizer`.`name`,\" \", `organizer`.`organizerID`)) AS Name',

],

I was able to access the other data as follow:

 [
          'label'=> 'End Date',
          'attribute' => 'event.endDate',

    ],

this prove that my data provider contains data, I have try a couple of ways but i still couldn't get to access on it. Can anyone help me on this?

1 Answers1

0

If you want to do some operation on DB and show the result on gridview, you must first define a variable on your model and use that on your gridview. It's not correct to use SQL query in gridview. The long story short, you can easily define a variable and a getter method in your model like this:

<?php

use yii\db\ActiveRecord;

class YourModel extends ActiveRecord
{
    public $organizerName;

    // Some AR Model codes go here!

    public function getOrganizerName()
    {
        /** 
         * Suppose you have a 1-1 relation which is called getOrganization
         * and you retrive your desired data about the organization from
         * that
         */
        return $this->organization->name 
               . ' ('
               . $this->organization->id
               . ')';
    }
}

Now you can use organizerName attribute in your model:

[
      'label'=> 'End Time',
      'attribute' => 'organizerName',

],

Note that if you want to do somethings like sorting or adding a search box, you must do some extra works for this field.

meysam
  • 1,754
  • 2
  • 20
  • 30