CGridView and CArrayDataProvider

Hello all,

I’m quite new to Yii and I’m still confused about CArrayDataProvider and CGridView (or CListView I guess).

Here’s my situation: I get raw data from PHPExcel, in the form of an array of arrays:




var_dump($rawData) :


array(32) { [1]=> array(5) { [0]=> string(10) "firstname1" [1]=> string(9) "lastname1" [2]=> string(15) "email@tutu.com1" [3]=> string(<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' /> "company1" [4]=> NULL } [2]=> array(5) { [0]=> string(10) "firstname2" [1]=> string(9) "lastname2" [2]=> string(15) "email@tutu.com2" [3]=> string(<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' /> "company2" [4]=> NULL } ...

This looks pretty straightforward to me.

So I create a CArrayDataProvider from that:


$arrayProvider=new CArrayDataProvider($rawData);

So far so good. Then I render:


$this->render('index',array(

			'arrayProvider' => $arrayProvider,

),

And my view is like:


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

	'dataProvider'=>$arrayProvider,

	));

I guess it should be right, but ar runtime I get a Cannot use a scalar value as an array error…

So I’m doing something wrong, but what ? Am I not using the right tool for the job ?

Thanks for the help !

There are few things you could check:

  1. provide ‘columns’ configuration

  2. CArraDataProvider needs ‘id’ field in every item. You can change its name (other than ‘id’) but still you have to provide one. Read more: http://www.yiiframework.com/wiki/378/an-important-tip-when-you-are-using-carraydataprovider/

Thanks for your answer!

That said, my array currently is like that :


array(32) { [1]=> array(5) { [0]=> string(10) "firstname1" [1]=> string(9) "lastname1" [2]=> string(15) "email@tutu.com1" [3]=> string(<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' /> "company1" [4]=> NULL } ... }

I don’t really have named keys, only indexes. How can I do then ? It’s confusing indeed.

I’m wondering if i’m really using the appropriate way. Is there something more straightforward ? Another type of view or data provider ?

Thanks !

OK apparently you have to have named keys, including one named ‘id’ indeed. This just doesn’t work if you have only indexes. So I had to name a look to rename all my keys, then it worked.

Weird, but well, now I know :)

Thanks

You save my day !

Thanks a lot !