Yii Framework Forum: Indirect modification of overloaded property Model::$relation has no effect - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

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

#1 User is offline   twisted1919 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 645
  • Joined: 23-October 10
  • Location:Romania

Posted 13 April 2012 - 01:04 PM

// 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
0

#2 User is offline   saegeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 245
  • Joined: 09-December 09
  • Location:Montpellier - France

Posted 03 May 2012 - 07:40 AM

maybe ?
$m=$this->metaFields;
foreach($m AS $field)
        {
        ...

And [for] their saying, "Indeed, we have killed the Messiah, Jesus, the son of Mary, the messenger of God ." And they did not kill him, nor did they crucify him; but [another] was made to resemble him to them. And indeed, those who differ over it are in doubt about it. They have no knowledge of it except the following of assumption. And they did not kill him, for certain.Rather, God raised him to Himself.
0

#3 User is offline   twisted1919 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 645
  • Joined: 23-October 10
  • Location:Romania

Posted 05 May 2012 - 07:09 PM

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.
1

#4 User is offline   elexperimento 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 12-March 11
  • Location:Bogotá, Colombia, South America

Posted 15 July 2013 - 04:07 PM

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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users