diff --git a/lib/Doctrine/ORM/EntityRepository.php b/lib/Doctrine/ORM/EntityRepository.php index da7e84ee4..2f0a32a86 100644 --- a/lib/Doctrine/ORM/EntityRepository.php +++ b/lib/Doctrine/ORM/EntityRepository.php @@ -1,4 +1,4 @@ -EntityRepository. - * + * * @param EntityManager $em The EntityManager to use. * @param ClassMetadata $classMetadata The class descriptor. */ @@ -63,11 +63,11 @@ class EntityRepository $this->_em = $em; $this->_class = $class; } - + /** * Create a new QueryBuilder instance that is prepopulated for this entity name * - * @param string $alias + * @param string $alias * @return QueryBuilder $qb */ public function createQueryBuilder($alias) @@ -76,7 +76,7 @@ class EntityRepository ->select($alias) ->from($this->_entityName, $alias); } - + /** * Clears the repository, causing all managed entities to become detached. */ @@ -84,7 +84,7 @@ class EntityRepository { $this->_em->clear($this->_class->rootEntityName); } - + /** * Finds an entity by its primary key / identifier. * @@ -118,23 +118,23 @@ class EntityRepository { return $this->findBy(array()); } - + /** * Finds entities by a set of criteria. * - * @param string $column - * @param string $value + * @param string $column + * @param string $value * @return array */ public function findBy(array $criteria) { return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->loadAll($criteria); } - + /** * Finds a single entity by a set of criteria. * - * @param string $column + * @param string $column * @param string $value * @return object */ @@ -142,7 +142,7 @@ class EntityRepository { return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->load($criteria); } - + /** * Adds support for magic finders. * @@ -160,11 +160,14 @@ class EntityRepository $by = substr($method, 9, strlen($method)); $method = 'findOneBy'; } else { - throw new \BadMethodCallException("Undefined method '$method'."); + throw new \BadMethodCallException( + "Undefined method '$method'. The method name must start with ". + "either findBy or findOneBy!" + ); } - + if ( ! isset($arguments[0])) { - throw DoctrineException::findByNameRequired(); + throw ORMException::findByRequiresParameter($method.$by); } $fieldName = lcfirst(\Doctrine\Common\Util\Inflector::classify($by)); @@ -172,7 +175,7 @@ class EntityRepository if ($this->_class->hasField($fieldName)) { return $this->$method(array($fieldName => $arguments[0])); } else { - throw \Doctrine\Common\DoctrineException::invalidFindBy($by); + throw ORMException::invalidFindByCall($this->_entityName, $fieldName, $method.$by); } } } \ No newline at end of file diff --git a/lib/Doctrine/ORM/ORMException.php b/lib/Doctrine/ORM/ORMException.php index 15cc3a6fb..0342f1546 100644 --- a/lib/Doctrine/ORM/ORMException.php +++ b/lib/Doctrine/ORM/ORMException.php @@ -4,7 +4,7 @@ namespace Doctrine\ORM; /** * Base exception class for all ORM exceptions. - * + * * @author Roman Borschel * @since 2.0 */ @@ -14,46 +14,57 @@ class ORMException extends \Exception { return new self("Entity of type " . get_class($entity) . " is missing an assigned ID."); } - + public static function unrecognizedField($field) { return new self("Unrecognized field: $field"); } - + public static function removedEntityInCollectionDetected($entity, $assoc) { return new self("Removed entity of type " . get_class($entity) . " detected in collection '" . $assoc->sourceFieldName . "' during flush." . " Remove deleted entities from collections."); } - + public static function invalidEntityState($state) { return new self("Invalid entity state: $state."); } - + public static function detachedEntityCannotBeRemoved() { return new self("A detached entity can not be removed."); } - + public static function invalidFlushMode($mode) { return new self("'$mode' is an invalid flush mode."); } - + public static function entityManagerClosed() { return new self("The EntityManager is closed."); } - + public static function invalidHydrationMode($mode) { return new self("'$mode' is an invalid hydration mode."); } - + public static function mismatchedEventManager() { return new self("Cannot use different EventManager instances for EntityManager and Connection."); } + + public static function findByRequiresParameter($methodName) { + return new self("You need to pass a parameter to '".$methodName."'"); + } + + public static function invalidFindByCall($entityName, $fieldName, $method) { + return new self( + "Entity '".$entityName."' has no field '".$fieldName."'. ". + "You can therefore not call '".$method."' on the entities' repository" + ); + } } diff --git a/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php b/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php index 0ffbc651c..f72de006c 100644 --- a/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php @@ -25,13 +25,13 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase $user->username = 'romanb'; $user->status = 'freak'; $this->_em->persist($user); - + $user2 = new CmsUser; $user2->name = 'Guilherme'; $user2->username = 'gblanco'; $user2->status = 'dev'; $this->_em->persist($user2); - + $this->_em->flush(); $user1Id = $user->getId(); unset($user); @@ -39,32 +39,48 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->_em->clear(); $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); - + $user = $repos->find($user1Id); $this->assertTrue($user instanceof CmsUser); $this->assertEquals('Roman', $user->name); $this->assertEquals('freak', $user->status); - + $this->_em->clear(); - + $users = $repos->findBy(array('status' => 'dev')); $this->assertEquals(1, count($users)); $this->assertTrue($users[0] instanceof CmsUser); $this->assertEquals('Guilherme', $users[0]->name); $this->assertEquals('dev', $users[0]->status); - + $this->_em->clear(); - + $users = $repos->findByStatus('dev'); $this->assertEquals(1, count($users)); $this->assertTrue($users[0] instanceof CmsUser); $this->assertEquals('Guilherme', $users[0]->name); $this->assertEquals('dev', $users[0]->status); - + $this->_em->clear(); - + $users = $repos->findAll(); - $this->assertEquals(2, count($users)); + $this->assertEquals(2, count($users)); + } + + /** + * @expectedException \Doctrine\ORM\ORMException + */ + public function testExceptionIsThrownWhenCallingFindByWithoutParameter() { + $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser') + ->findByStatus(); + } + + /** + * @expectedException \Doctrine\ORM\ORMException + */ + public function testExceptionIsThrownWhenUsingInvalidFieldName() { + $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser') + ->findByThisFieldDoesNotExist('testvalue'); } }