Displaying data in grid view

Hi, my yii2 table goes in this way.


CREATE TABLE `ad_analytics` (

  `id` int(90) NOT NULL,

  `ad_id` int(90) NOT NULL,

  `advertiser_id` int(90) NOT NULL,

  `publisher_id` int(90) NOT NULL,

  `visitor_ip` varchar(250) NOT NULL,

  `type_ad` varchar(90) NOT NULL,

  `impression` int(90) NOT NULL,

  `view` int(90) NOT NULL,

  `clicks` int(90) NOT NULL,

  `date_event` date NOT NULL,

  `placed_date` date NOT NULL,

  `ending_date` date NOT NULL,

  `cpc` int(60) NOT NULL,

  `cpv` int(60) NOT NULL,

  `cpi` int(60) NOT NULL

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

And here is my grid view i want to display custom data in grid view for that the query is goes here.


<?= GridView::widget([

      'dataProvider'=>new ActiveDataProvider([

          'query' => Adanalytics::find()->

                where(['advertiser_id' =>  Yii::$app->user->identity->id ]),

        ]),

        'filterModel' => $searchModel,

        'columns' => [

            ['class' => 'yii\grid\SerialColumn'],


            'id',

            'ad_id',

            'advertiser_id',

            'publisher_id',

            'visitor_ip',

            //'type_ad',

            //'impression',

            //'view',

            //'clicks',

            //'date',


            ['class' => 'yii\grid\ActionColumn'],

        ],

    ]); ?>

And my query is as follows.


SELECT id, ad_id,MAX(impression) AS imp, MAX(view) AS vi, MAX(clicks) AS cl FROM ad_analytics GROUP BY ad_id, visitor_ip;

How to excecute this query in grid view and show the data accordingly?

Just build this query in YourSearchModel::search()




  $query = AdAnalytics::find();

  $query->select('...');

  $query->from('...');

  $query->groupBy('...');



Where is this file is locatted?

appnamespace\models\YourSearchModel

It is showing.

Call to undefined function frontend\models\select()

The code I have executed


 $query = Adanalytics::find();

        $query = select('id, ad_id,MAX(impression), MAX(view), MAX(clicks)');

        $query = from('ad_analytics');

        $query = groupBy('ad_id, visitor_ip');

Once again look at my code more carefully

This is my search model query is getting executed fine but the result table looks same. How to customise the result now.

Can someone refer me a documentation which I can look into?




namespace frontend\models;


use Yii;

use yii\base\Model;

use yii\data\ActiveDataProvider;

use frontend\models\Adanalytics;


/**

 * Adanalytics2Search represents the model behind the search form of `frontend\models\Adanalytics`.

 */

class Adanalytics2Search extends Adanalytics

{

    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            [['id', 'ad_id', 'advertiser_id', 'publisher_id', 'impression', 'view', 'clicks', 'cpc', 'cpv', 'cpi'], 'integer'],

            [['visitor_ip', 'type_ad', 'date_event', 'placed_date', 'ending_date'], 'safe'],

        ];

    }


    /**

     * @inheritdoc

     */

    public function scenarios()

    {

        // bypass scenarios() implementation in the parent class

        return Model::scenarios();

    }


    /**

     * Creates data provider instance with search query applied

     *

     * @param array $params

     *

     * @return ActiveDataProvider

     */

    public function search($params)

    {

         $query = Adanalytics::find();

         $query->select('id');

         $query->select('ad_id');

         $query->select('MAX(impression) AS im');

         $query->select('MAX(view) AS vi');

         $query->select('MAX(clicks) AS cl');

         $query->from('ad_analytics');

         $query->groupBy('visitor_ip');

         $query->groupBy('ad_id');

        //$query = "SELECT id, ad_id,MAX(impression) AS imp, MAX(view) AS vi, MAX(clicks) AS cl FROM ad_analytics GROUP BY ad_id, visitor_ip;";

        // $query = from('ad_analytics');

        // $query = groupBy('ad_id, visitor_ip');


        // add conditions that should always apply here


        $dataProvider = new ActiveDataProvider([

            'query' => $query,

        ]);


        $this->load($params);


        if (!$this->validate()) {

            // uncomment the following line if you do not want to return any records when validation fails

            // $query->where('0=1');

            return $dataProvider;

        }


        // grid filtering conditions

        $query->andFilterWhere([

            'publisher_id' => $this->publisher_id

        ]);


        return $dataProvider;

    }

}

Putting the same code in view has solved this issue :)