/**
  * {@inheritdoc}
  */
 public function loadById(array $identifier, $entity = null)
 {
     $cacheKey = new EntityCacheKey($this->class->rootEntityName, $identifier);
     $cacheEntry = $this->region->get($cacheKey);
     $class = $this->class;
     if ($cacheEntry !== null) {
         if ($cacheEntry->class !== $this->class->name) {
             $class = $this->metadataFactory->getMetadataFor($cacheEntry->class);
         }
         if (($entity = $this->hydrator->loadCacheEntry($class, $cacheKey, $cacheEntry, $entity)) !== null) {
             if ($this->cacheLogger) {
                 $this->cacheLogger->entityCacheHit($this->regionName, $cacheKey);
             }
             return $entity;
         }
     }
     $entity = $this->persister->loadById($identifier, $entity);
     if ($entity === null) {
         return null;
     }
     $class = $this->class;
     $className = ClassUtils::getClass($entity);
     if ($className !== $this->class->name) {
         $class = $this->metadataFactory->getMetadataFor($className);
     }
     $cacheEntry = $this->hydrator->buildCacheEntry($class, $cacheKey, $entity);
     $cached = $this->region->put($cacheKey, $cacheEntry);
     if ($cached && ($this->joinedAssociations === null || count($this->joinedAssociations) > 0)) {
         $this->storeJoinedAssociations($entity);
     }
     if ($this->cacheLogger) {
         if ($cached) {
             $this->cacheLogger->entityCachePut($this->regionName, $cacheKey);
         }
         $this->cacheLogger->entityCacheMiss($this->regionName, $cacheKey);
     }
     return $entity;
 }
Exemplo n.º 2
0
 /**
  * Creates a closure capable of finalizing state a cloned proxy
  *
  * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $classMetadata
  * @param \Doctrine\ORM\Persisters\Entity\EntityPersister    $entityPersister
  *
  * @return \Closure
  *
  * @throws \Doctrine\ORM\EntityNotFoundException
  */
 private function createCloner(ClassMetadata $classMetadata, EntityPersister $entityPersister)
 {
     return function (BaseProxy $proxy) use($entityPersister, $classMetadata) {
         if ($proxy->__isInitialized()) {
             return;
         }
         $proxy->__setInitialized(true);
         $proxy->__setInitializer(null);
         $class = $entityPersister->getClassMetadata();
         $identifier = $classMetadata->getIdentifierValues($proxy);
         $original = $entityPersister->loadById($identifier);
         if (null === $original) {
             throw EntityNotFoundException::fromClassNameAndIdentifier($classMetadata->getName(), $this->identifierFlattener->flattenIdentifier($classMetadata, $identifier));
         }
         foreach ($class->getReflectionClass()->getProperties() as $property) {
             if (!$class->hasField($property->name) && !$class->hasAssociation($property->name)) {
                 continue;
             }
             $property->setAccessible(true);
             $property->setValue($proxy, $property->getValue($original));
         }
     };
 }