Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Extract more messages into ORMInvalidArgumentException

  • Loading branch information...
commit 719e05e53e9e376c7d4138a1d20070ece2cfc57b 1 parent a8906ce
Benjamin Eberlei authored October 22, 2011
20  lib/Doctrine/ORM/ORMInvalidArgumentException.php
@@ -19,6 +19,11 @@
19 19
 
20 20
 namespace Doctrine\ORM;
21 21
 
  22
+/**
  23
+ * Contains exception messages for all invalid lifecycle state exceptions inside UnitOfWork
  24
+ *
  25
+ * @author Benjamin Eberlei <kontakt@beberlei.de>
  26
+ */
22 27
 class ORMInvalidArgumentException extends \InvalidArgumentException
23 28
 {
24 29
     static public function scheduleInsertForManagedEntity($entity)
@@ -74,6 +79,21 @@ static public function entityNotManaged($entity)
74 79
                 "from the database or registered as new through EntityManager#persist");
75 80
     }
76 81
 
  82
+    static public function entityHasNoIdentity($entity, $operation)
  83
+    {
  84
+        throw new self("Entity has no identity, therefore " . $operation ." cannot be performed. " . self::objToStr($entity));
  85
+    }
  86
+
  87
+    static public function entityIsRemoved($entity, $operation)
  88
+    {
  89
+        throw new self("Entity is removed, therefore " . $operation ." cannot be performed. " . self::objToStr($entity));
  90
+    }
  91
+
  92
+    static public function detachedEntityCannot($entity, $operation)
  93
+    {
  94
+        throw new self("A detached entity was found during " . $operation . " " . self::objToStr($entity));
  95
+    }
  96
+
77 97
     /**
78 98
      * Helper method to show an object as string.
79 99
      *
14  lib/Doctrine/ORM/UnitOfWork.php
@@ -935,10 +935,10 @@ public function scheduleForUpdate($entity)
935 935
     {
936 936
         $oid = spl_object_hash($entity);
937 937
         if ( ! isset($this->entityIdentifiers[$oid])) {
938  
-            throw new InvalidArgumentException("Entity has no identity." . self::objToStr($entity));
  938
+            throw ORMInvalidArgumentException::entityHasNoIdentity($entity, "scheduling for update");
939 939
         }
940 940
         if (isset($this->entityDeletions[$oid])) {
941  
-            throw new InvalidArgumentException("Entity is removed." . self::objToStr($entity));
  941
+            throw ORMInvalidArgumentException::entityIsRemoved($entity, "schedule for update");
942 942
         }
943 943
 
944 944
         if ( ! isset($this->entityUpdates[$oid]) && ! isset($this->entityInsertions[$oid])) {
@@ -1160,7 +1160,7 @@ public function removeFromIdentityMap($entity)
1160 1160
         $classMetadata = $this->em->getClassMetadata(get_class($entity));
1161 1161
         $idHash = implode(' ', $this->entityIdentifiers[$oid]);
1162 1162
         if ($idHash === '') {
1163  
-            throw new InvalidArgumentException("The given entity has no identity." . self::objToStr($entity));
  1163
+            throw ORMInvalidArgumentException::entityHasNoIdentity($entity, "remove from identity map");
1164 1164
         }
1165 1165
         $className = $classMetadata->rootEntityName;
1166 1166
         if (isset($this->identityMap[$className][$idHash])) {
@@ -1291,7 +1291,7 @@ private function doPersist($entity, array &$visited)
1291 1291
                 break;
1292 1292
             case self::STATE_DETACHED:
1293 1293
                 // Can actually not happen right now since we assume STATE_NEW.
1294  
-                throw new InvalidArgumentException("Detached entity passed to persist()." . self::objToStr($entity));
  1294
+                throw ORMInvalidArgumentException::detachedEntityCannot($entity, "persisted");
1295 1295
             default:
1296 1296
                 throw new UnexpectedValueException("Unexpected entity state: $entityState." . self::objToStr($entity));
1297 1297
         }
@@ -1350,7 +1350,7 @@ private function doRemove($entity, array &$visited)
1350 1350
                 $this->scheduleForDelete($entity);
1351 1351
                 break;
1352 1352
             case self::STATE_DETACHED:
1353  
-                throw new InvalidArgumentException("A detached entity can not be removed." . self::objToStr($entity));
  1353
+                throw ORMInvalidArgumentException::detachedEntityCannot($entity, "removed");
1354 1354
             default:
1355 1355
                 throw new UnexpectedValueException("Unexpected entity state: $entityState." . self::objToStr($entity));
1356 1356
         }
@@ -1417,9 +1417,7 @@ private function doMerge($entity, array &$visited, $prevManagedCopy = null, $ass
1417 1417
                 if ($managedCopy) {
1418 1418
                     // We have the entity in-memory already, just make sure its not removed.
1419 1419
                     if ($this->getEntityState($managedCopy) == self::STATE_REMOVED) {
1420  
-                        throw new InvalidArgumentException(
1421  
-                                'Removed entity ' . self::objToStr($managedCopy) . ' detected during merge.'
1422  
-                                . ' Can not merge with a removed entity.');
  1420
+                        throw ORMInvalidArgumentException::entityIsRemoved($managedCopy, "merge");
1423 1421
                     }
1424 1422
                 } else {
1425 1423
                     // We need to fetch the managed copy in order to merge.

0 notes on commit 719e05e

Please sign in to comment.
Something went wrong with that request. Please try again.