Customised query to display in CGridView in yii

1.5k Views Asked by At

Controller,

    $model=Product::model()->display_products_statistics();
$this->render('admin',array(
    'model'=>$model,
));

View,

 $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'product-grid',
'dataProvider' => $model->display_products_statistics(),
'filter'=> $model,
'columns'=> array(
    'member_count',
    'seller_count',
    'visitor_count',
    'lowest price',
    'desc',
    'price',
    'createdate',
    'updatedate',
    'opid',
    'pimg',
    array(
        'class'=>'CButtonColumn',
    ),
),
     ));

Model,

$sql="select member_count,seller_count,
visitor_count from fc_product fp
group by  member_count,visitor_count,seller_count
order by member_count desc";
$connection=Yii::app()->db;
$command=$connection->createCommand($sql);
$products_statistics=$command->queryAll();
$dataprovider=new CArrayDataProvider($products_statistics,
    array('pagination'=>false));
return $dataprovider->getData();

But then I am getting this error

Fatal error: Call to a member function getData() on a non-object in D:\wamp\www\yii\framework\zii\widgets\CBaseListView.php on line 111

I am confused and don't know what's going wrong.

1

There are 1 best solutions below

0
Joe Miller On BEST ANSWER

Your problem is that you are not using CDataProvider in the correct way. CGridView needs an instance of CDataProvider to display, but you have used CDataProvider->getData() instead. I'd also recommend using CSqlDataProvider, which is purpose made for custom sql queries. Try this in your model;

$count = Yii::app()->db->createCommand('SELECT COUNT(*) FROM fc_product')->queryScalar();
$sql="select member_count,seller_count,
visitor_count from fc_product fp
group by  member_count,visitor_count,seller_count
order by member_count desc";
return new CSqlDataProvider($sql, array(
    'totalItemCount' => $count,
    'sort' => array(
        'attributes' => array(
             'member_count', 'visitor_count', 'seller_count',
        ),
    ),
    'pagination' => false