phpyiicgridviewcactivedataprovider

CGridView for custom CActiveDataProvider select


So the problem is, that I have a view that will render a CGridView containing a count value. I had to make a custom CDbCriteria so that I could select the right infos. Now I can't display it in a CGridView.

Here is my controller action that builds the CActiveDataProvider:

public function actionListProducts(){
    $criteria=new CDbCriteria;
    $criteria->select = 'rp_product as product, count(rp_id) as cnt';
    $criteria->group = 'product';

    $dataProvider=new CActiveDataProvider('report', array( 'criteria'=>$criteria,
    ));

    $this->render('listProducts',array(
        'dataProvider'=>$dataProvider,
    ));
}

I am trying to display it in this CGridView:

$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'reports-grid',
'dataProvider'=>$dataProvider,
'columns'=>array(
        'product',
        'cnt'
    ),
));

I know the data is fetched properly because my CGridView has the right number of rows... What am I suppose to write in the 'columns' description?

Thank you


Solution

  • If you want to use the aliases product and cnt (var AS alias), you have to make a public property of it in the model Report.

    Example:

    Model extends CActiveRecord {
    ....
    public $product;
    public $cnt;
    ...
    }