Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Extract more messages into ORMInvalidArgumentException

  • Loading branch information...
commit 719e05e53e9e376c7d4138a1d20070ece2cfc57b 1 parent a8906ce
@beberlei beberlei authored
View
20 lib/Doctrine/ORM/ORMInvalidArgumentException.php
@@ -19,6 +19,11 @@
namespace Doctrine\ORM;
+/**
+ * Contains exception messages for all invalid lifecycle state exceptions inside UnitOfWork
+ *
+ * @author Benjamin Eberlei <kontakt@beberlei.de>
+ */
class ORMInvalidArgumentException extends \InvalidArgumentException
@Koc
Koc added a note

Why not like in ZF2 or Symfony2? http://ralphschindler.com/2010/09/15/exception-best-practices-in-php-5-3 (with interface)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
{
static public function scheduleInsertForManagedEntity($entity)
@@ -74,6 +79,21 @@ static public function entityNotManaged($entity)
"from the database or registered as new through EntityManager#persist");
}
+ static public function entityHasNoIdentity($entity, $operation)
+ {
+ throw new self("Entity has no identity, therefore " . $operation ." cannot be performed. " . self::objToStr($entity));
+ }
+
+ static public function entityIsRemoved($entity, $operation)
+ {
+ throw new self("Entity is removed, therefore " . $operation ." cannot be performed. " . self::objToStr($entity));
+ }
+
+ static public function detachedEntityCannot($entity, $operation)
+ {
+ throw new self("A detached entity was found during " . $operation . " " . self::objToStr($entity));
+ }
+
/**
* Helper method to show an object as string.
*
View
14 lib/Doctrine/ORM/UnitOfWork.php
@@ -935,10 +935,10 @@ public function scheduleForUpdate($entity)
{
$oid = spl_object_hash($entity);
if ( ! isset($this->entityIdentifiers[$oid])) {
- throw new InvalidArgumentException("Entity has no identity." . self::objToStr($entity));
+ throw ORMInvalidArgumentException::entityHasNoIdentity($entity, "scheduling for update");
}
if (isset($this->entityDeletions[$oid])) {
- throw new InvalidArgumentException("Entity is removed." . self::objToStr($entity));
+ throw ORMInvalidArgumentException::entityIsRemoved($entity, "schedule for update");
}
if ( ! isset($this->entityUpdates[$oid]) && ! isset($this->entityInsertions[$oid])) {
@@ -1160,7 +1160,7 @@ public function removeFromIdentityMap($entity)
$classMetadata = $this->em->getClassMetadata(get_class($entity));
$idHash = implode(' ', $this->entityIdentifiers[$oid]);
if ($idHash === '') {
- throw new InvalidArgumentException("The given entity has no identity." . self::objToStr($entity));
+ throw ORMInvalidArgumentException::entityHasNoIdentity($entity, "remove from identity map");
}
$className = $classMetadata->rootEntityName;
if (isset($this->identityMap[$className][$idHash])) {
@@ -1291,7 +1291,7 @@ private function doPersist($entity, array &$visited)
break;
case self::STATE_DETACHED:
// Can actually not happen right now since we assume STATE_NEW.
- throw new InvalidArgumentException("Detached entity passed to persist()." . self::objToStr($entity));
+ throw ORMInvalidArgumentException::detachedEntityCannot($entity, "persisted");
default:
throw new UnexpectedValueException("Unexpected entity state: $entityState." . self::objToStr($entity));
}
@@ -1350,7 +1350,7 @@ private function doRemove($entity, array &$visited)
$this->scheduleForDelete($entity);
break;
case self::STATE_DETACHED:
- throw new InvalidArgumentException("A detached entity can not be removed." . self::objToStr($entity));
+ throw ORMInvalidArgumentException::detachedEntityCannot($entity, "removed");
default:
throw new UnexpectedValueException("Unexpected entity state: $entityState." . self::objToStr($entity));
}
@@ -1417,9 +1417,7 @@ private function doMerge($entity, array &$visited, $prevManagedCopy = null, $ass
if ($managedCopy) {
// We have the entity in-memory already, just make sure its not removed.
if ($this->getEntityState($managedCopy) == self::STATE_REMOVED) {
- throw new InvalidArgumentException(
- 'Removed entity ' . self::objToStr($managedCopy) . ' detected during merge.'
- . ' Can not merge with a removed entity.');
+ throw ORMInvalidArgumentException::entityIsRemoved($managedCopy, "merge");
}
} else {
// We need to fetch the managed copy in order to merge.
Please sign in to comment.
Something went wrong with that request. Please try again.