2

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?

tereško
  • 58,060
  • 25
  • 98
  • 150
k to the z
  • 3,217
  • 2
  • 27
  • 41

3 Answers3

3

findAll() returns an array, whereas CActiveDataProvider returns a dataProvider.

If you want to use find all, all you have to do is convert the array using CArrayDataProvider

example:

<?
php $this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>new CArrayDataProvider($list, array()),
 )); 
?>
Neil McGuigan
  • 46,580
  • 12
  • 123
  • 152
2

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.

k to the z
  • 3,217
  • 2
  • 27
  • 41
Samuel Barbosa
  • 782
  • 7
  • 18
  • Would the new CActiveDataProvider call be in the view with the widget? – k to the z Apr 26 '11 at 20:08
  • Answering my own comment: no. You call the dataprovider in the controller and pass the variable to the view. This worked. Thank you sir. – k to the z Apr 26 '11 at 20:14
  • that's totally wrong. you do not need to use a CActiveDataProvider – Neil McGuigan Apr 27 '11 at 17:28
  • It's not totally wrong. I changed the "have to" to "can". You can use either. "totally wrong" is far to extreme as was your downvote. You should have just edited his question. I didn't end up using findall. I used his example that just requires you to pass the AR model. – k to the z Apr 27 '11 at 19:31
0

Using CArrayDataProvider is also a option. But the sorting is easy to do when it comes from CActiveDataProvider.

Samuel Barbosa
  • 782
  • 7
  • 18