/**
  * @param string|null $message
  */
 public function __construct($message = null)
 {
     parent::__construct();
     if ($message) {
         $this->message = $message;
     }
 }
 public function testFromClassNameAndIdentifier()
 {
     $exception = EntityNotFoundException::fromClassNameAndIdentifier('foo', array('foo' => 'bar'));
     $this->assertInstanceOf('Doctrine\\ORM\\EntityNotFoundException', $exception);
     $this->assertSame('Entity of type \'foo\' for IDs foo(bar) was not found', $exception->getMessage());
     $exception = EntityNotFoundException::fromClassNameAndIdentifier('foo', array());
     $this->assertInstanceOf('Doctrine\\ORM\\EntityNotFoundException', $exception);
     $this->assertSame('Entity of type \'foo\' was not found', $exception->getMessage());
 }
示例#3
0
 /**
  * Executes a merge operation on an entity.
  *
  * @param object      $entity
  * @param array       $visited
  * @param object|null $prevManagedCopy
  * @param array|null  $assoc
  *
  * @return object The managed copy of the entity.
  *
  * @throws OptimisticLockException If the entity uses optimistic locking through a version
  *         attribute and the version check against the managed copy fails.
  * @throws ORMInvalidArgumentException If the entity instance is NEW.
  * @throws EntityNotFoundException
  */
 private function doMerge($entity, array &$visited, $prevManagedCopy = null, $assoc = null)
 {
     $oid = spl_object_hash($entity);
     if (isset($visited[$oid])) {
         $managedCopy = $visited[$oid];
         if ($prevManagedCopy !== null) {
             $this->updateAssociationWithMergedEntity($entity, $assoc, $prevManagedCopy, $managedCopy);
         }
         return $managedCopy;
     }
     $class = $this->em->getClassMetadata(get_class($entity));
     // First we assume DETACHED, although it can still be NEW but we can avoid
     // an extra db-roundtrip this way. If it is not MANAGED but has an identity,
     // we need to fetch it from the db anyway in order to merge.
     // MANAGED entities are ignored by the merge operation.
     $managedCopy = $entity;
     if ($this->getEntityState($entity, self::STATE_DETACHED) !== self::STATE_MANAGED) {
         // Try to look the entity up in the identity map.
         $id = $class->getIdentifierValues($entity);
         // If there is no ID, it is actually NEW.
         if (!$id) {
             $managedCopy = $this->newInstance($class);
             $this->persistNew($class, $managedCopy);
         } else {
             $flatId = $class->containsForeignIdentifier ? $this->identifierFlattener->flattenIdentifier($class, $id) : $id;
             $managedCopy = $this->tryGetById($flatId, $class->rootEntityName);
             if ($managedCopy) {
                 // We have the entity in-memory already, just make sure its not removed.
                 if ($this->getEntityState($managedCopy) == self::STATE_REMOVED) {
                     throw ORMInvalidArgumentException::entityIsRemoved($managedCopy, "merge");
                 }
             } else {
                 // We need to fetch the managed copy in order to merge.
                 $managedCopy = $this->em->find($class->name, $flatId);
             }
             if ($managedCopy === null) {
                 // If the identifier is ASSIGNED, it is NEW, otherwise an error
                 // since the managed entity was not found.
                 if (!$class->isIdentifierNatural()) {
                     throw EntityNotFoundException::fromClassNameAndIdentifier($class->getName(), $this->identifierFlattener->flattenIdentifier($class, $id));
                 }
                 $managedCopy = $this->newInstance($class);
                 $class->setIdentifierValues($managedCopy, $id);
                 $this->persistNew($class, $managedCopy);
             }
         }
         if ($class->isVersioned) {
             $reflField = $class->reflFields[$class->versionField];
             $managedCopyVersion = $reflField->getValue($managedCopy);
             $entityVersion = $reflField->getValue($entity);
             // Throw exception if versions don't match.
             if ($managedCopyVersion != $entityVersion) {
                 throw OptimisticLockException::lockFailedVersionMismatch($entity, $entityVersion, $managedCopyVersion);
             }
         }
         $visited[$oid] = $managedCopy;
         // mark visited
         if (!($entity instanceof Proxy && !$entity->__isInitialized())) {
             if ($managedCopy instanceof Proxy && !$managedCopy->__isInitialized()) {
                 $managedCopy->__load();
             }
             $this->mergeEntityStateIntoManagedCopy($entity, $managedCopy);
         }
         if ($class->isChangeTrackingDeferredExplicit()) {
             $this->scheduleForDirtyCheck($entity);
         }
     }
     if ($prevManagedCopy !== null) {
         $this->updateAssociationWithMergedEntity($entity, $assoc, $prevManagedCopy, $managedCopy);
     }
     // Mark the managed copy visited as well
     $visited[spl_object_hash($managedCopy)] = $managedCopy;
     $this->cascadeMerge($entity, $managedCopy, $visited);
     return $managedCopy;
 }
示例#4
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));
         }
     };
 }