From bac6f73d0be352e92932d1eedadaacce21f47f92 Mon Sep 17 00:00:00 2001 From: joschi127 <127.joschi@gmail.com> Date: Sat, 26 Sep 2015 17:15:29 +0000 Subject: [PATCH] Fix loading references if mapped by uuid and not by id. DocumentManager::getReference() does not work correctly if called with an uuid instead of an id, so if we have an uuid here, we have to fetch the node first (which unfortunately is not optimal from a performance point of view) to get the correct id of the document. (DocumentManager::getReference() seems to work first - but it will initialize the id field with the uuid and this will cause errors later, e.g. when accessing other referenced documents of the loaded document - it is simply not possible to use it correctly with an uuid) --- lib/Doctrine/ORM/ODMAdapter/UnitOfWork.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/Doctrine/ORM/ODMAdapter/UnitOfWork.php b/lib/Doctrine/ORM/ODMAdapter/UnitOfWork.php index f5bfa4c..af5f0fd 100644 --- a/lib/Doctrine/ORM/ODMAdapter/UnitOfWork.php +++ b/lib/Doctrine/ORM/ODMAdapter/UnitOfWork.php @@ -10,6 +10,8 @@ use Doctrine\ORM\ODMAdapter\Event\ManagerEventArgs; use Doctrine\ORM\ODMAdapter\Exception\UnitOfWorkException; use Doctrine\ORM\ODMAdapter\Mapping\ClassMetadata; +use PHPCR\ItemNotFoundException; +use PHPCR\Util\UUIDHelper; /** * Unit of work class @@ -472,6 +474,24 @@ public function loadReferences($object) continue; } + if (UUIDHelper::isUUID($objectValue)) { + // DocumentManager::getReference() does not work correctly if called with an uuid instead of an id, + // so if we have an uuid here, we have to fetch the node first (which unfortunately is not optimal + // from a performance point of view) to get the correct id of the document. + // (DocumentManager::getReference() seems to work first - but it will initialize the id field with the + // uuid and this will cause errors later, e.g. when accessing other referenced documents of the loaded + // document - it is simply not possible to use it correctly with an uuid) + try { + $referencedNode = $this->objectAdapterManager + ->getManager($object, $fieldName) + ->getPhpcrSession() + ->getNodeByIdentifier($objectValue); + + $objectValue = $referencedNode->getPath(); + } catch (ItemNotFoundException $e) { + continue; + } + } $referencedObject = $this->objectAdapterManager ->getManager($object, $fieldName)