By Value and by Reference
By default, Doctrine hydrator works by value. This means that the hydrator will access and modify properties through the public API of an entity (that is, with getters and setters). You can override this behaviour to work by reference (the hydrator will access the properties through the Reflection API and bypass your getters and setters).
To change the behaviour from by value to by reference, set the second parameter of the constructor to false:
To illustrate the difference between the two, consider an extraction with the given entity:
Using the hydrator by value:
1 use Doctrine\Laminas\Hydrator\DoctrineObject as DoctrineHydrator;
$hydrator = new DoctrineHydrator($objectManager);
$object = new SimpleEntity();
$object->setFoo('bar');
$data = $hydrator->extract($object);
echo $data['foo']; // never executed, because the script was killed when getter was accessed
2
3
4
5
6
7
8
9
The hydrator used the public API getFoo()
to
retrieve the value.
Using the hydrator by reference:
It prints bar
, showing that the getter was not called.