How to reference or save a reference in an Active Record attribute?

Is there any possibility to reference an ActiveRecord attribute?


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

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)

Have you tried it?

For me it throws the same error:

 Indirect modification of overloaded property xyz has no effect 

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.