getEntityPersister() public method

Gets the EntityPersister for an Entity.
public getEntityPersister ( string $entityName ) : Doctrine\ORM\Persister\AbstractEntityPersister
$entityName string The name of the Entity.
return Doctrine\ORM\Persister\AbstractEntityPersister
 /**
  * {@inheritdoc}
  */
 public function loadCacheEntry(ClassMetadata $metadata, EntityCacheKey $key, EntityCacheEntry $entry, $entity = null)
 {
     $data = $entry->data;
     $hints = self::$hints;
     if ($entity !== null) {
         $hints[Query::HINT_REFRESH] = true;
         $hints[Query::HINT_REFRESH_ENTITY] = $entity;
     }
     foreach ($metadata->associationMappings as $name => $assoc) {
         if (!isset($assoc['cache']) || !isset($data[$name])) {
             continue;
         }
         $assocClass = $data[$name]->class;
         $assocId = $data[$name]->identifier;
         $isEagerLoad = $assoc['fetch'] === ClassMetadata::FETCH_EAGER || $assoc['type'] === ClassMetadata::ONE_TO_ONE && !$assoc['isOwningSide'];
         if (!$isEagerLoad) {
             $data[$name] = $this->em->getReference($assocClass, $assocId);
             continue;
         }
         $assocKey = new EntityCacheKey($assoc['targetEntity'], $assocId);
         $assocPersister = $this->uow->getEntityPersister($assoc['targetEntity']);
         $assocRegion = $assocPersister->getCacheRegion();
         $assocEntry = $assocRegion->get($assocKey);
         if ($assocEntry === null) {
             return null;
         }
         $data[$name] = $this->uow->createEntity($assocEntry->class, $assocEntry->resolveAssociationEntries($this->em), $hints);
     }
     if ($entity !== null) {
         $this->uow->registerManaged($entity, $key->identifier, $data);
     }
     $result = $this->uow->createEntity($entry->class, $data, $hints);
     $this->uow->hydrationComplete();
     return $result;
 }
 /**
  * @param \Doctrine\ORM\Cache\QueryCacheKey $key
  * @param array                             $assoc
  * @param mixed                             $assocValue
  *
  * @return array|null
  */
 private function storeAssociationCache(QueryCacheKey $key, array $assoc, $assocValue)
 {
     $assocPersister = $this->uow->getEntityPersister($assoc['targetEntity']);
     $assocMetadata = $assocPersister->getClassMetadata();
     $assocRegion = $assocPersister->getCacheRegion();
     // Handle *-to-one associations
     if ($assoc['type'] & ClassMetadata::TO_ONE) {
         $assocIdentifier = $this->uow->getEntityIdentifier($assocValue);
         $entityKey = new EntityCacheKey($assocMetadata->rootEntityName, $assocIdentifier);
         if (!$assocValue instanceof Proxy && $key->cacheMode & Cache::MODE_REFRESH || !$assocRegion->contains($entityKey)) {
             // Entity put fail
             if (!$assocPersister->storeEntityCache($assocValue, $entityKey)) {
                 return null;
             }
         }
         return ['targetEntity' => $assocMetadata->rootEntityName, 'identifier' => $assocIdentifier, 'type' => $assoc['type']];
     }
     // Handle *-to-many associations
     $list = [];
     foreach ($assocValue as $assocItemIndex => $assocItem) {
         $assocIdentifier = $this->uow->getEntityIdentifier($assocItem);
         $entityKey = new EntityCacheKey($assocMetadata->rootEntityName, $assocIdentifier);
         if ($key->cacheMode & Cache::MODE_REFRESH || !$assocRegion->contains($entityKey)) {
             // Entity put fail
             if (!$assocPersister->storeEntityCache($assocItem, $entityKey)) {
                 return null;
             }
         }
         $list[$assocItemIndex] = $assocIdentifier;
     }
     return ['targetEntity' => $assocMetadata->rootEntityName, 'type' => $assoc['type'], 'list' => $list];
 }
 /**
  * {@inheritdoc}
  */
 public function storeCollectionCache(CollectionCacheKey $key, $elements)
 {
     $targetPersister = $this->uow->getEntityPersister($this->targetEntity->rootEntityName);
     $targetRegion = $targetPersister->getCacheRegion();
     $targetHydrator = $targetPersister->getEntityHydrator();
     $entry = $this->hydrator->buildCacheEntry($this->targetEntity, $key, $elements);
     foreach ($entry->identifiers as $index => $identifier) {
         $entityKey = new EntityCacheKey($this->targetEntity->rootEntityName, $identifier);
         if ($targetRegion->contains($entityKey)) {
             continue;
         }
         $class = $this->targetEntity;
         $className = ClassUtils::getClass($elements[$index]);
         if ($className !== $this->targetEntity->name) {
             $class = $this->metadataFactory->getMetadataFor($className);
         }
         $entity = $elements[$index];
         $entityEntry = $targetHydrator->buildCacheEntry($class, $entityKey, $entity);
         $targetRegion->put($entityKey, $entityEntry);
     }
     $cached = $this->region->put($key, $entry);
     if ($this->cacheLogger && $cached) {
         $this->cacheLogger->collectionCachePut($this->regionName, $key);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function loadCacheEntry(ClassMetadata $metadata, CollectionCacheKey $key, CollectionCacheEntry $entry, PersistentCollection $collection)
 {
     $assoc = $metadata->associationMappings[$key->association];
     $targetPersister = $this->uow->getEntityPersister($assoc['targetEntity']);
     $targetRegion = $targetPersister->getCacheRegion();
     $list = array();
     foreach ($entry->identifiers as $index => $identifier) {
         $entityEntry = $targetRegion->get(new EntityCacheKey($assoc['targetEntity'], $identifier));
         if ($entityEntry === null) {
             return null;
         }
         $list[$index] = $this->uow->createEntity($entityEntry->class, $entityEntry->data, self::$hints);
     }
     array_walk($list, function ($entity, $index) use($collection) {
         $collection->hydrateSet($index, $entity);
     });
     return $list;
 }
 /**
  * @param string $targetEntity
  * @param object $element
  */
 protected function evictElementCache($targetEntity, $element)
 {
     /* @var $targetPersister CachedEntityPersister */
     $targetPersister = $this->uow->getEntityPersister($targetEntity);
     $targetRegion = $targetPersister->getCacheRegion();
     $key = new EntityCacheKey($targetEntity, $this->uow->getEntityIdentifier($element));
     $targetRegion->evict($key);
     if ($this->cacheLogger) {
         $this->cacheLogger->entityCachePut($targetRegion->getName(), $key);
     }
 }
Example #6
0
 /**
  * {@inheritdoc}
  */
 public function evictEntityRegions()
 {
     $metadatas = $this->em->getMetadataFactory()->getAllMetadata();
     foreach ($metadatas as $metadata) {
         $persister = $this->uow->getEntityPersister($metadata->rootEntityName);
         if (!$persister instanceof CachedPersister) {
             continue;
         }
         $persister->getCacheRegion()->evictAll();
     }
 }
 /**
  * {@inheritdoc}
  */
 public function loadCacheEntry(ClassMetadata $metadata, CollectionCacheKey $key, CollectionCacheEntry $entry, PersistentCollection $collection)
 {
     $assoc = $metadata->associationMappings[$key->association];
     /* @var $targetPersister \Doctrine\ORM\Cache\Persister\CachedPersister */
     $targetPersister = $this->uow->getEntityPersister($assoc['targetEntity']);
     $targetRegion = $targetPersister->getCacheRegion();
     $list = [];
     $entityEntries = $targetRegion->getMultiple($entry);
     if ($entityEntries === null) {
         return null;
     }
     /* @var $entityEntries \Doctrine\ORM\Cache\EntityCacheEntry[] */
     foreach ($entityEntries as $index => $entityEntry) {
         $list[$index] = $this->uow->createEntity($entityEntry->class, $entityEntry->resolveAssociationEntries($this->em), self::$hints);
     }
     array_walk($list, function ($entity, $index) use($collection) {
         $collection->hydrateSet($index, $entity);
     });
     $this->uow->hydrationComplete();
     return $list;
 }
 /**
  * @param object $entity
  */
 private function storeJoinedAssociations($entity)
 {
     if ($this->joinedAssociations === null) {
         $associations = array();
         foreach ($this->class->associationMappings as $name => $assoc) {
             if (isset($assoc['cache']) && $assoc['type'] & ClassMetadata::TO_ONE && ($assoc['fetch'] === ClassMetadata::FETCH_EAGER || !$assoc['isOwningSide'])) {
                 $associations[] = $name;
             }
         }
         $this->joinedAssociations = $associations;
     }
     foreach ($this->joinedAssociations as $name) {
         $assoc = $this->class->associationMappings[$name];
         $assocEntity = $this->class->getFieldValue($entity, $name);
         if ($assocEntity === null) {
             continue;
         }
         $assocId = $this->uow->getEntityIdentifier($assocEntity);
         $assocKey = new EntityCacheKey($assoc['targetEntity'], $assocId);
         $assocPersister = $this->uow->getEntityPersister($assoc['targetEntity']);
         $assocPersister->storeEntityCache($assocEntity, $assocKey);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function put(QueryCacheKey $key, ResultSetMapping $rsm, $result, array $hints = array())
 {
     if ($rsm->scalarMappings) {
         throw new CacheException("Second level cache does not support scalar results.");
     }
     if (count($rsm->entityMappings) > 1) {
         throw new CacheException("Second level cache does not support multiple root entities.");
     }
     if (!$rsm->isSelect) {
         throw new CacheException("Second-level cache query supports only select statements.");
     }
     if (isset($hints[Query::HINT_FORCE_PARTIAL_LOAD]) && $hints[Query::HINT_FORCE_PARTIAL_LOAD]) {
         throw new CacheException("Second level cache does not support partial entities.");
     }
     if (!($key->cacheMode & Cache::MODE_PUT)) {
         return false;
     }
     $data = array();
     $entityName = reset($rsm->aliasMap);
     $hasRelation = !empty($rsm->relationMap);
     $metadata = $this->em->getClassMetadata($entityName);
     $persister = $this->uow->getEntityPersister($entityName);
     if (!$persister instanceof CachedPersister) {
         throw CacheException::nonCacheableEntity($entityName);
     }
     $region = $persister->getCacheRegion();
     foreach ($result as $index => $entity) {
         $identifier = $this->uow->getEntityIdentifier($entity);
         $data[$index]['identifier'] = $identifier;
         $data[$index]['associations'] = array();
         if ($key->cacheMode & Cache::MODE_REFRESH || !$region->contains($entityKey = new EntityCacheKey($entityName, $identifier))) {
             // Cancel put result if entity put fail
             if (!$persister->storeEntityCache($entity, $entityKey)) {
                 return false;
             }
         }
         if (!$hasRelation) {
             continue;
         }
         // @TODO - move to cache hydration components
         foreach ($rsm->relationMap as $name) {
             $assoc = $metadata->associationMappings[$name];
             if (($assocValue = $metadata->getFieldValue($entity, $name)) === null || $assocValue instanceof Proxy) {
                 continue;
             }
             if (!isset($assoc['cache'])) {
                 throw CacheException::nonCacheableEntityAssociation($entityName, $name);
             }
             $assocPersister = $this->uow->getEntityPersister($assoc['targetEntity']);
             $assocRegion = $assocPersister->getCacheRegion();
             $assocMetadata = $assocPersister->getClassMetadata();
             // Handle *-to-one associations
             if ($assoc['type'] & ClassMetadata::TO_ONE) {
                 $assocIdentifier = $this->uow->getEntityIdentifier($assocValue);
                 if ($key->cacheMode & Cache::MODE_REFRESH || !$assocRegion->contains($entityKey = new EntityCacheKey($assocMetadata->rootEntityName, $assocIdentifier))) {
                     // Cancel put result if association entity put fail
                     if (!$assocPersister->storeEntityCache($assocValue, $entityKey)) {
                         return false;
                     }
                 }
                 $data[$index]['associations'][$name] = array('targetEntity' => $assocMetadata->rootEntityName, 'identifier' => $assocIdentifier, 'type' => $assoc['type']);
                 continue;
             }
             // Handle *-to-many associations
             $list = array();
             foreach ($assocValue as $assocItemIndex => $assocItem) {
                 $assocIdentifier = $this->uow->getEntityIdentifier($assocItem);
                 if ($key->cacheMode & Cache::MODE_REFRESH || !$assocRegion->contains($entityKey = new EntityCacheKey($assocMetadata->rootEntityName, $assocIdentifier))) {
                     // Cancel put result if entity put fail
                     if (!$assocPersister->storeEntityCache($assocItem, $entityKey)) {
                         return false;
                     }
                 }
                 $list[$assocItemIndex] = $assocIdentifier;
             }
             $data[$index]['associations'][$name] = array('targetEntity' => $assocMetadata->rootEntityName, 'type' => $assoc['type'], 'list' => $list);
         }
     }
     return $this->region->put($key, new QueryCacheEntry($data));
 }
 /**
  * @override
  */
 public function getEntityPersister($entityName)
 {
     return isset($this->_persisterMock[$entityName]) ? $this->_persisterMock[$entityName] : parent::getEntityPersister($entityName);
 }
Example #11
-1
 public function onFlush(OnFlushEventArgs $eventArgs)
 {
     if (!$this->active) {
         return;
     }
     // Clear updateData
     $this->updateData = $this->extraUpdates = array();
     $this->em = $eventArgs->getEntityManager();
     $this->conn = $this->em->getConnection();
     $this->uow = $this->em->getUnitOfWork();
     $this->platform = $this->conn->getDatabasePlatform();
     $this->revisionId = null;
     // reset revision
     $this->draft = false;
     $processedEntities = array();
     foreach ($this->uow->getScheduledEntityDeletions() as $entity) {
         if (!$this->annotationReader->isRevised(get_class($entity), true)) {
             continue;
         }
         //doctrine is fine deleting elements multiple times. We are not.
         $hash = $this->getHash($entity);
         if (in_array($hash, $processedEntities)) {
             continue;
         }
         $processedEntities[] = $hash;
         $this->extraUpdates[spl_object_hash($entity)] = $entity;
         $persister = $this->uow->getEntityPersister(get_class($entity));
         $this->updateData[spl_object_hash($entity)] = $this->prepareUpdateData($persister, $entity);
         $entityData = array_merge($this->getOriginalEntityData($entity), $this->uow->getEntityIdentifier($entity));
         $this->saveRevisionEntityData($this->em->getClassMetadata(get_class($entity)), $entityData, 'DEL');
         if ($this->annotationReader->isDraft($entity) && $entity->isDraft()) {
             $this->resetRevisedData($entity);
             $this->setRevisionInfo($entity);
             $persister = $this->uow->getEntityPersister(get_class($entity));
             $this->updateData[spl_object_hash($entity)] = $this->prepareUpdateData($persister, $entity);
             $fieldName = 'deletedAt';
             $reflProp = new \ReflectionProperty($entity, $fieldName);
             $reflProp->setAccessible(true);
             $oldValue = $reflProp->getValue($entity);
             $reflProp->setValue($entity, null);
             $this->uow->scheduleExtraUpdate($entity, array($fieldName => array($oldValue, null)));
         }
         if (isset($this->softDeletes[spl_object_hash($entity)])) {
             $this->em->persist($entity);
         }
     }
     foreach ($this->uow->getScheduledEntityInsertions() as $entity) {
         if (!$this->annotationReader->isRevised(get_class($entity), true)) {
             continue;
         }
         $this->setRevisionInfo($entity);
         $this->extraUpdates[spl_object_hash($entity)] = $entity;
         $persister = $this->uow->getEntityPersister(get_class($entity));
         $this->updateData[spl_object_hash($entity)] = $this->prepareUpdateData($persister, $entity);
         if ($this->annotationReader->isDraft($entity) && $entity->isDraft()) {
             $this->insertDrafts[spl_object_hash($entity)] = $entity;
             $this->resetRevisedData($entity);
             $this->uow->recomputeSingleEntityChangeSet($this->em->getClassMetadata(get_class($entity)), $entity);
         }
     }
     foreach ($this->uow->getScheduledEntityUpdates() as $entity) {
         if (!$this->annotationReader->isRevised(get_class($entity), true)) {
             continue;
         }
         $this->setRevisionInfo($entity);
         $this->extraUpdates[spl_object_hash($entity)] = $entity;
         $persister = $this->uow->getEntityPersister(get_class($entity));
         $this->updateData[spl_object_hash($entity)] = $this->prepareUpdateData($persister, $entity);
         if ($this->annotationReader->isDraft($entity) && $entity->isDraft()) {
             $this->resetRevisedData($entity);
         }
     }
 }