Example #1
0
 /**
  * {@inheritDoc}
  */
 public function getReference($entityName, $id)
 {
     $class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\'));
     if (!is_array($id)) {
         $id = array($class->identifier[0] => $id);
     }
     $sortedId = array();
     foreach ($class->identifier as $identifier) {
         if (!isset($id[$identifier])) {
             throw ORMException::missingIdentifierField($class->name, $identifier);
         }
         $sortedId[$identifier] = $id[$identifier];
         unset($id[$identifier]);
     }
     if ($id) {
         throw ORMException::unrecognizedIdentifierFields($class->name, array_keys($id));
     }
     // Check identity map first, if its already in there just return it.
     $flattenedId = $this->unitOfWork->flattenIdentifier($class, $sortedId);
     if (($entity = $this->unitOfWork->tryGetById($flattenedId, $class->rootEntityName)) !== false) {
         return $entity instanceof $class->name ? $entity : null;
     }
     if ($class->subClasses) {
         return $this->find($entityName, $sortedId);
     }
     $entity = $this->proxyFactory->getProxy($class->name, $flattenedId);
     $this->unitOfWork->registerManaged($entity, $flattenedId, array());
     return $entity;
 }