diff --git a/lib/Doctrine/ORM/ORMInvalidArgumentException.php b/lib/Doctrine/ORM/ORMInvalidArgumentException.php index b4edc5dd2..a4a03c41a 100644 --- a/lib/Doctrine/ORM/ORMInvalidArgumentException.php +++ b/lib/Doctrine/ORM/ORMInvalidArgumentException.php @@ -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. * diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index 2b696063f..a80079041 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -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); diff --git a/tests/Doctrine/Tests/ORM/Functional/Locking/LockTest.php b/tests/Doctrine/Tests/ORM/Functional/Locking/LockTest.php index 6917e7252..62c787409 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Locking/LockTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/Locking/LockTest.php @@ -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); }