1
0
mirror of synced 2025-03-20 23:13:57 +03:00

Extract more messages into ORMInvalidArgumentException

This commit is contained in:
Benjamin Eberlei 2011-10-22 12:57:55 +02:00
parent a8906ce572
commit 719e05e53e
2 changed files with 26 additions and 8 deletions

View File

@ -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
{
static public function scheduleInsertForManagedEntity($entity)
@ -74,6 +79,21 @@ class ORMInvalidArgumentException extends \InvalidArgumentException
"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 File

@ -935,10 +935,10 @@ class UnitOfWork implements PropertyChangedListener
{
$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 @@ class UnitOfWork implements PropertyChangedListener
$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 @@ class UnitOfWork implements PropertyChangedListener
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 @@ class UnitOfWork implements PropertyChangedListener
$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 @@ class UnitOfWork implements PropertyChangedListener
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.