1
0
Fork 0
mirror of synced 2025-04-01 12:26:11 +03:00

Stringify entity in all UnitOfWork exceptions

This commit is contained in:
Benjamin Eberlei 2011-10-22 12:49:33 +02:00
parent 5392737de4
commit a8906ce572
3 changed files with 19 additions and 12 deletions

View file

@ -68,6 +68,12 @@ class ORMInvalidArgumentException extends \InvalidArgumentException
. "during cascading a persist operation.");
}
static public function entityNotManaged($entity)
{
throw new self("Entity " . self::objToStr($entity) . " is not managed. An entity is managed if its fetched " .
"from the database or registered as new through EntityManager#persist");
}
/**
* Helper method to show an object as string.
*

View file

@ -653,7 +653,7 @@ class UnitOfWork implements PropertyChangedListener
$oid = spl_object_hash($entity);
if ( ! isset($this->entityStates[$oid]) || $this->entityStates[$oid] != self::STATE_MANAGED) {
throw new InvalidArgumentException("Entity of type '" . $class->name . "' must be managed.");
throw ORMInvalidArgumentException::entityNotManaged($entity);
}
/* TODO: Just return if changetracking policy is NOTIFY?
@ -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.");
throw new InvalidArgumentException("Entity has no identity." . self::objToStr($entity));
}
if (isset($this->entityDeletions[$oid])) {
throw new InvalidArgumentException("Entity is removed.");
throw new InvalidArgumentException("Entity is removed." . self::objToStr($entity));
}
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.");
throw new InvalidArgumentException("The given entity has no identity." . self::objToStr($entity));
}
$className = $classMetadata->rootEntityName;
if (isset($this->identityMap[$className][$idHash])) {
@ -1291,9 +1291,9 @@ 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().");
throw new InvalidArgumentException("Detached entity passed to persist()." . self::objToStr($entity));
default:
throw new UnexpectedValueException("Unexpected entity state: $entityState.");
throw new UnexpectedValueException("Unexpected entity state: $entityState." . self::objToStr($entity));
}
$this->cascadePersist($entity, $visited);
@ -1350,9 +1350,9 @@ class UnitOfWork implements PropertyChangedListener
$this->scheduleForDelete($entity);
break;
case self::STATE_DETACHED:
throw new InvalidArgumentException("A detached entity can not be removed.");
throw new InvalidArgumentException("A detached entity can not be removed." . self::objToStr($entity));
default:
throw new UnexpectedValueException("Unexpected entity state: $entityState.");
throw new UnexpectedValueException("Unexpected entity state: $entityState." . self::objToStr($entity));
}
}
@ -1417,7 +1417,8 @@ 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 detected during merge.'
throw new InvalidArgumentException(
'Removed entity ' . self::objToStr($managedCopy) . ' detected during merge.'
. ' Can not merge with a removed entity.');
}
} else {
@ -1624,7 +1625,7 @@ class UnitOfWork implements PropertyChangedListener
$entity
);
} else {
throw new InvalidArgumentException("Entity is not MANAGED.");
throw ORMInvalidArgumentException::entityNotManaged($entity);
}
$this->cascadeRefresh($entity, $visited);
@ -1789,7 +1790,7 @@ class UnitOfWork implements PropertyChangedListener
public function lock($entity, $lockMode, $lockVersion = null)
{
if ($this->getEntityState($entity, self::STATE_DETACHED) != self::STATE_MANAGED) {
throw new InvalidArgumentException("Entity is not MANAGED.");
throw ORMInvalidArgumentException::entityNotManaged($entity);
}
$entityName = get_class($entity);

View file

@ -71,7 +71,7 @@ class LockTest extends \Doctrine\Tests\OrmFunctionalTestCase {
public function testLockUnmanagedEntity_ThrowsException() {
$article = new CmsArticle();
$this->setExpectedException('InvalidArgumentException', 'Entity is not MANAGED.');
$this->setExpectedException('InvalidArgumentException', 'Entity Doctrine\Tests\Models\CMS\CmsArticle');
$this->_em->lock($article, LockMode::OPTIMISTIC, $article->version + 1);
}