CDbDataReader and CGridView

Hello, I am trying to build a "report" view, so in order to use a custom SQL command, I used Data Access Objects (DAO), like this:


$command = Yii::app()->db->createCommand();

// construction of custom SQL command here using Yii Query Builder syntax

Using this approach, at the end I get a CDbDataReader:


$dataReader = $command->query();

Now, I would like to show the data in a CGridView, but it will only accept a data provider like CActiveDataProvider. For example:


$dataProvider=new CActiveDataProvider('Post');


$this->widget('zii.widgets.grid.CGridView', array(

    'dataProvider'=>$dataProvider,

));

How could I use the dataReader to show data in the CGridView? Is there any way to "convert" a CDbDataReader into a data provider? Or am I definitely forced to avoid the CGridView in this case?

I would like to avoid having to manually build my CGridView-like table fetching the query results using a php foreach.

Suggestion for a different approach are welcome too!

Thanks in advance!

Read more about CSqlDataProvider and CArrayDataProvider.

/Tommy

Thanks Tommy for your answer. It was very useful!

I decided to use CSqlDataProvider and it is working OK, but I can’t make reference to the $data property on the CGridView definition, which I can use just fine if I use a CActiveDataProvider as the CGridView data provider.




$this->widget('zii.widgets.grid.CGridView', array(

    'dataProvider'=>$dataProvider,

	'columns'=>array(

		array(

			'name'=>'field1',

			'header'=>' My Field 1',

		),

		array(

			'header'=>'My Field 1 Again',

			'value'=>'$data->field1',

		),

	),

));

In this example, the column “My Field 1” works OK, but the column “My Field 1 Again” shows blank data. Am I doing something wrong? I have to use the ‘value’ property because I need to make additional formatting work to the value I am getting from the data provider.

You have to use




 'value'=>'$data["field1"]',



with these two providers.

/Tommy

Great! It is working now.

Thank you very much Tommy for your help!