"get_Class() Expects Parameter 1 To Be Object, Array Given"

I have the following code:


$dp=new CArrayDataProvider($model->person->emails);

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

              array('dataProvider'=>$dp,

                    'columns'=>array('address',

                    array('class'=>'CButtonColumn'))));

Where $model->person->emails is


public function relations()

{

    return array(

        'emails' => array(self::HAS_MANY, 'Email', 'person_id'),

// etc.

Why, when I click the "edit" button of an entry in the resulting grid, do I get this "get_Class() Expects Parameter 1 To Be Object, Array Given" error?

Hi Heyho

is the $model the default object that generated by crud ?

In this case you could try something like that


$db =$model->search();

$db = $db->getData();

$db = $db[0]->manyItems;

$db = new CArrayDataProvider($db);


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

              array('dataProvider'=>$dp,

                    'columns'=>array('myattribute',

                    array('class'=>'CButtonColumn'))));

I just tested and it works in my case :)

I don’t see how it can have worked. $dp would be null.

What is manyItems?

please post your action of controller.

if $dp is null you have to wrap my code


if ($dp) {

 $db =$model->search();

 $db = $db->getData();

 $db = $db[0]->manyItems;

 $db = new CArrayDataProvider($db);

} else {

 $db = new CArrayDataProvider(array());

}

manyItems is an example of my code not yours!

in your project could be the same of $model->person(s)

so you have to find out how can ajusts my code for your needs

You never set $dp to anything!

Heyho,

I post a sample of code that I tried but on copy-paste I change a bracket


$db =$model->search();

if ($db) {

 $db = $db->getData();

 $db = $db[0]->manyItems;

 $db = new CArrayDataProvider($db);

} else {

 $db = new CArrayDataProvider(array());

}

check also by yourself line by line if something gives null or empty

@KonApaz

Heyho is complaining that you set $db but not $dp.

@Heyho

You could have imagined that KonApaz had typed a simplest typo and made a necessary change.

But, on the issue stated in the 1st post, KonApaz’s suggestion seems to be irrelevant. I think it might be because of the difference between CActiveDataProvider and CArrayDataProvider. The “Edit” button seems to try to get the id by “$data->id”. It will work when you use CActiveDataProvider. But it must be “$data[‘id’]” when you use CArrayDataProvider.