phpmysqlyiiyii-componentszii-widgets

Inserting Active Record Data into Yii's CGRidView widget


I have a table in mysql that I want to display with the CGridView widget. Here is my code thus far:

My Controller file (snipped of course):

public function actionIndex()
{
  //call the AR table model
  $model = new ViewResults();
  //This generates a simple "SELECT * FROM table statment".
  $list = $model->findAll();
  $this->render('index', array('list'=>$list));
}

My View file looks like(snipped):

<?php $this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$list,
 )); ?>

I'm getting the following error:

Call to a member function getData() on a non-object in C:\xampp\framework\zii\widgets\CBaseListView.php on line 105

Here is the source code to the CBaseListView.php file.

I'm pretty sure I'm screwing up by putting the list object in the widget. Is there something I have to do to $list before I pass it to the widget?


Solution

  • You can use the CActiveDataProvider Yii's Class. Something like this:

    $dataProvider = new CActiveDataProvider('model', array(
      'criteria'=>array(
        'order'=>'column1',
      ),
    ));
    
    $this->widget('zii.widgets.grid.CGridView', array(
      'id'=>'my-grid',
      'dataProvider'=>$dataProvider,
      'columns'=>array(
        'column1',
        'column2',
        'column3',
        array(
          'class'=>'CButtonColumn',
        ),
      ),
    ));
    

    Where model is your model and columns your columns.