yiibie
(Sonstiges28)
August 19, 2012, 3:17pm
1
Is there any possibility to reference an ActiveRecord attribute?
Example:
$model = new MyActiveRecord();
$model->id = 42;
$reference = &$model->id;
Saving of a reference in an attribute is not possible, neither:
$model = new MyActiveRecord();
$related = new RelatedModel();
$model->id = &$related;
I think that it will be pretty hard without modifying the framework code.
Haensel
(Johannes)
August 19, 2012, 3:29pm
2
You could add a method beginning with & like
public function &getIdByReference()
{
return $this->id;
}
That will of course kill the magic getter functionality (unless you override the parent __get() method to take that into account)
yiibie
(Sonstiges28)
August 19, 2012, 3:32pm
3
Haensel:
You could add a method beginning with & like
public function &getIdByReference()
{
return $this->id;
}
That will of course kill the magic getter functionality
Have you tried it?
For me it throws the same error:
Indirect modification of overloaded property xyz has no effect
Haensel
(Johannes)
August 19, 2012, 3:35pm
4
Ah, interesting, didn’t know that. Thought it would be worth a try. I guess the problem lies within the __get() method used which will only return a read only property. so instead of $this->id which would cause the getter to load the variable you could try $this->getAttribute(‘id’) which will return $this->_attributes[‘id’] if I am correct.