예제 #1
0
 /**
  * Returns the object with the (internal) identifier, if it is known to the
  * backend. Otherwise NULL is returned.
  *
  * @param mixed $identifier
  * @param string $objectType
  * @param boolean $useLazyLoading This option is ignored in this persistence manager
  * @return object The object for the identifier if it is known, or NULL
  * @api
  */
 public function getObjectByIdentifier($identifier, $objectType = null, $useLazyLoading = false)
 {
     if (isset($this->newObjects[$identifier])) {
         return $this->newObjects[$identifier];
     }
     if ($this->persistenceSession->hasIdentifier($identifier)) {
         return $this->persistenceSession->getObjectByIdentifier($identifier);
     } else {
         $objectData = $this->backend->getObjectDataByIdentifier($identifier, $objectType);
         if ($objectData !== false) {
             return $this->dataMapper->mapToObject($objectData);
         } else {
             return null;
         }
     }
 }
 /**
  * Remove objects removed from SplObjectStorage compared to
  * $previousSplObjectStorage.
  *
  * @param \SplObjectStorage $splObjectStorage
  * @param array $previousObjectStorage
  * @return void
  */
 protected function removeDeletedSplObjectStorageEntries(\SplObjectStorage $splObjectStorage = null, array $previousObjectStorage)
 {
     // remove objects detached since reconstitution
     foreach ($previousObjectStorage as $item) {
         if ($splObjectStorage instanceof LazySplObjectStorage && !$this->persistenceSession->hasIdentifier($item['value']['identifier'])) {
             // ingore this identifier, assume it was blocked by security query rewriting upon activation
             continue;
         }
         $object = $this->persistenceSession->getObjectByIdentifier($item['value']['identifier']);
         if ($splObjectStorage === null || !$splObjectStorage->contains($object)) {
             if ($this->reflectionService->getClassSchema($object)->getModelType() === ClassSchema::MODELTYPE_ENTITY && $this->reflectionService->getClassSchema($object)->isAggregateRoot() === false) {
                 $this->removeEntity($object);
             } elseif ($this->reflectionService->getClassSchema($object)->getModelType() === ClassSchema::MODELTYPE_VALUEOBJECT) {
                 $this->removeValueObject($object);
             }
         }
     }
 }
 /**
  * Maps a single record into the object it represents and registers it as
  * reconstituted with the session.
  *
  * @param array $objectData
  * @return object
  * @throws Exception\InvalidObjectDataException
  * @throws PersistenceException
  */
 public function mapToObject(array $objectData)
 {
     if ($objectData === []) {
         throw new Exception\InvalidObjectDataException('The array with object data was empty, probably object not found or access denied.', 1277974338);
     }
     if ($this->persistenceSession->hasIdentifier($objectData['identifier'])) {
         return $this->persistenceSession->getObjectByIdentifier($objectData['identifier']);
     } else {
         $className = $objectData['classname'];
         $classSchema = $this->reflectionService->getClassSchema($className);
         $object = unserialize('O:' . strlen($className) . ':"' . $className . '":0:{};');
         $this->persistenceSession->registerObject($object, $objectData['identifier']);
         if ($classSchema->getModelType() === ClassSchema::MODELTYPE_ENTITY) {
             $this->persistenceSession->registerReconstitutedEntity($object, $objectData);
         }
         if ($objectData['properties'] === []) {
             if (!$classSchema->isLazyLoadableObject()) {
                 throw new PersistenceException('The object of type "' . $className . '" is not marked as lazy loadable.', 1268309017);
             }
             $persistenceManager = $this->persistenceManager;
             $persistenceSession = $this->persistenceSession;
             $dataMapper = $this;
             $identifier = $objectData['identifier'];
             $modelType = $classSchema->getModelType();
             $object->Flow_Persistence_LazyLoadingObject_thawProperties = function ($object) use($persistenceManager, $persistenceSession, $dataMapper, $identifier, $modelType) {
                 $objectData = $persistenceManager->getObjectDataByIdentifier($identifier);
                 $dataMapper->thawProperties($object, $identifier, $objectData);
                 if ($modelType === ClassSchema::MODELTYPE_ENTITY) {
                     $persistenceSession->registerReconstitutedEntity($object, $objectData);
                 }
             };
         } else {
             $this->thawProperties($object, $objectData['identifier'], $objectData);
         }
         return $object;
     }
 }
 /**
  * @test
  */
 public function getIdentifierByObjectReturnsValueOfPropertyTaggedWithId()
 {
     $object = $this->createMock(ProxyInterface::class);
     $object->Persistence_Object_Identifier = 'randomlyGeneratedUuid';
     $object->customId = 'customId';
     $mockReflectionService = $this->createMock(ReflectionService::class);
     $mockReflectionService->expects($this->any())->method('getPropertyNamesByTag')->will($this->returnValue(['customId']));
     $session = new Persistence\Generic\Session();
     $session->injectReflectionService($mockReflectionService);
     $this->assertEquals('customId', $session->getIdentifierByObject($object));
 }