Indirect modification of overloaded property Model::$relation has no effect




// Entity model

[...]

public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'metaFields' => array(self::HAS_MANY, 'MetaField', 'entity_id', 'order'=>'sort_order ASC'),

            'modules' => array(self::MANY_MANY, 'EntityMod', '{{entity_to_entity_mod}}(entity_id, module_id)'),

            'activeModules' => array(self::MANY_MANY, 'EntityMod', '{{entity_to_entity_mod}}(entity_id, module_id)', 'condition'=>'`status`=:st','params'=>array(':st'=>self::STATUS_ACTIVE)),

            'htmlBlocks' => array(self::MANY_MANY, 'HtmlBlock', '{{html_block_to_entity}}(entity_id, block_id)'),

		);

	}




public function getMetaField($str)

    {

        static $loadedFields=array();

        if(isset($loadedFields[$str]))

            return $loadedFields[$str];

        

        if(empty($this->metaFields))

            return null;

        

        foreach($this->metaFields AS $field)

        {

            if($field->key===$str||$field->field_id===$str||$field->label===$str)

            {

                $loadedFields[$str]=$field->value;

                return $loadedFields[$str];

            }

        }

        return null;

    }


[...]



Calling:




<?php echo $model->getMetaField('meta-title');?>



Throws




Indirect modification of overloaded property Entity::$metaFields has no effect



The error is thrown when the execution reaches the foreach() loop, and i think it is happening because of the __get() magic method implementation from CActiveRecord.

Shouldn’t yii be php > 5.1.0 compatible and this error should never happen ?

This happens on Linux Debian, PHP 5.2.0, Apache 2.2.x, with Yii 1.1.10

maybe ?




$m=$this->metaFields;

foreach($m AS $field)

        {

        ...



Yeah, i know about that workaround, but i don’t like it at all.

I ended up by upgrading the php installation.

They should point out this issue somewhere in the docs.

It’s hard for somebody that wrote an entire application with $model->relation->property to rewrite it as $m=$model->relation; $m->property just because this issue isn’t highlighted anywhere.

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