Indirect modification of overloaded element

i am trying to pass a model object to a module, modify it, and then return the result,

but seem unable to modify it, getting: Indirect modification of overloaded element error

eg. in my controller i have:

$artist = Lessons::update($artist);

then in my module class i would have something like:

class Lessons {

public function update($_artist){

$_artist[‘details’][‘name’] = “bob”;

return $_artist;

}

}

but gives an error.

also tried:

$_artist->details->setAttribute(‘name’, ‘bob’);

but also no luck…

any ideas?

What is artist? I presume a model… .and what is $artist->details? a relation ?

details is a column in the table.

So $artist->details is a column in the table…

and you can set it with


$artist->detail = 'bob';

but in your code you use


$model['details']['name'] = 'bob';

um, oops. really sorry, was multitasking when i answered that.

actually details is a table relation -> artist has many details and name is column.

if i do an simple $model[‘details’][‘name’] = ‘bob’; sometimes it works and sometimes it doesn’t and i need to use setAttribute.

i’ve always been assuming $model[‘details’][‘name’] is the same as $model->details->name is this incorrect?

thanks for the help

Basically


$model->details

gets the value of the property "detail" from the object $model - $model is an OBJECT


$model['detail']

gets the value of the element "detail" from the array $model - $model is an ARRAY

When you say sometime it works and sometime not… it’s always with the same relation or there are different relations in question? or even some model attributes?

ah, thanks, worked it out.

turned out there was some data/attributes in the $model array that was not part of the model. which was breaking things. lesson learned

About another possibility with this very same error:

The same error message happened to me when I was trying to set the CActiveRecord Attributes array.

So I discovered another method to overcome this issue, in a case where the magic method is related to an object variable which contains an array take a look: you create an AUXILIARY ARRAY in which you put the original and the new values (sometimes one wants to REPLACE a value related to one of the keys, and these methods are not satisfactory). And AFTERWARDS use an assignation, which works like the reference. For example, in the next example, I generalize $model->attribute to

"Object->array_built_with_magic", but I used it first with $model->attribute. Take a look:

$auxiliary_array = array();

foreach(Object->array_built_with_magic as $key=>$value) {

if(….) {

$auxiliary_array[$key] = Object->array_built_with_magic[$key];

} else if (…) {

$auxiliary_array[$key] = $NEW_VALUE

}

}

//So now we have the array $auxiliary_array with the

// desired MIX (that is, some originals, some modifications)

//So we will do now:

Object->array_built_with_magic =$auxiliary_array;

I hope it’s useful, regards.

Note: please moderators RESTRAIN from saying “this is an old thread”. It’s quite common to find new solutions after several years from an old problem.

David López

Investigación y Programación