Posts

Doctrine deserialize entity and merge recursively

When you serialize doctrine entity and later unserialize you all lazy loaded associations are gone. Initializers won't fetch the data from database as well. One approach could be eagerly fetch all the associations you will need after deserialization. But there is another and more easy and memory effective way. You can do merge() on your entity to "restore" it. But you need to merge each entity you plan to use that could be quite annoying and sometimes impossible. So save your efforts and use recursive merging function above: <?php use Doctrine\ORM\EntityManager; class RecursiveMerge { /** * @var EntityManager */ protected $em; public function __construct ( EntityManager $entityManager ) { $this -> em = $entityManager ; } /** * Merge entity recursively * * @param object $entity * @param int $depth recursion depth to avoid cyclic references issue * * @return object