Commit 96a79b62 authored by guilhermeblanco's avatar guilhermeblanco

[2.0][DDC-362] Fixed missing namespace declaration on __call method of...

[2.0][DDC-362] Fixed missing namespace declaration on __call method of EntityRepository. Thanks Marcel Walter for the patch.
parent 50190c64
<?php <?php
/* /*
* $Id$ * $Id$
* *
...@@ -24,7 +24,7 @@ namespace Doctrine\ORM; ...@@ -24,7 +24,7 @@ namespace Doctrine\ORM;
/** /**
* An EntityRepository serves as a repository for entities with generic as well as * An EntityRepository serves as a repository for entities with generic as well as
* business specific methods for retrieving entities. * business specific methods for retrieving entities.
* *
* This class is designed for inheritance and users can subclass this class to * This class is designed for inheritance and users can subclass this class to
* write their own repositories with business-specific methods to locate entities. * write their own repositories with business-specific methods to locate entities.
* *
...@@ -50,10 +50,10 @@ class EntityRepository ...@@ -50,10 +50,10 @@ class EntityRepository
* @var Doctrine\ORM\Mapping\ClassMetadata * @var Doctrine\ORM\Mapping\ClassMetadata
*/ */
protected $_class; protected $_class;
/** /**
* Initializes a new <tt>EntityRepository</tt>. * Initializes a new <tt>EntityRepository</tt>.
* *
* @param EntityManager $em The EntityManager to use. * @param EntityManager $em The EntityManager to use.
* @param ClassMetadata $classMetadata The class descriptor. * @param ClassMetadata $classMetadata The class descriptor.
*/ */
...@@ -63,11 +63,11 @@ class EntityRepository ...@@ -63,11 +63,11 @@ class EntityRepository
$this->_em = $em; $this->_em = $em;
$this->_class = $class; $this->_class = $class;
} }
/** /**
* Create a new QueryBuilder instance that is prepopulated for this entity name * Create a new QueryBuilder instance that is prepopulated for this entity name
* *
* @param string $alias * @param string $alias
* @return QueryBuilder $qb * @return QueryBuilder $qb
*/ */
public function createQueryBuilder($alias) public function createQueryBuilder($alias)
...@@ -76,7 +76,7 @@ class EntityRepository ...@@ -76,7 +76,7 @@ class EntityRepository
->select($alias) ->select($alias)
->from($this->_entityName, $alias); ->from($this->_entityName, $alias);
} }
/** /**
* Clears the repository, causing all managed entities to become detached. * Clears the repository, causing all managed entities to become detached.
*/ */
...@@ -84,7 +84,7 @@ class EntityRepository ...@@ -84,7 +84,7 @@ class EntityRepository
{ {
$this->_em->clear($this->_class->rootEntityName); $this->_em->clear($this->_class->rootEntityName);
} }
/** /**
* Finds an entity by its primary key / identifier. * Finds an entity by its primary key / identifier.
* *
...@@ -118,23 +118,23 @@ class EntityRepository ...@@ -118,23 +118,23 @@ class EntityRepository
{ {
return $this->findBy(array()); return $this->findBy(array());
} }
/** /**
* Finds entities by a set of criteria. * Finds entities by a set of criteria.
* *
* @param string $column * @param string $column
* @param string $value * @param string $value
* @return array * @return array
*/ */
public function findBy(array $criteria) public function findBy(array $criteria)
{ {
return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->loadAll($criteria); return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->loadAll($criteria);
} }
/** /**
* Finds a single entity by a set of criteria. * Finds a single entity by a set of criteria.
* *
* @param string $column * @param string $column
* @param string $value * @param string $value
* @return object * @return object
*/ */
...@@ -142,7 +142,7 @@ class EntityRepository ...@@ -142,7 +142,7 @@ class EntityRepository
{ {
return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->load($criteria); return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->load($criteria);
} }
/** /**
* Adds support for magic finders. * Adds support for magic finders.
* *
...@@ -160,11 +160,14 @@ class EntityRepository ...@@ -160,11 +160,14 @@ class EntityRepository
$by = substr($method, 9, strlen($method)); $by = substr($method, 9, strlen($method));
$method = 'findOneBy'; $method = 'findOneBy';
} else { } 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])) { if ( ! isset($arguments[0])) {
throw DoctrineException::findByNameRequired(); throw ORMException::findByRequiresParameter($method.$by);
} }
$fieldName = lcfirst(\Doctrine\Common\Util\Inflector::classify($by)); $fieldName = lcfirst(\Doctrine\Common\Util\Inflector::classify($by));
...@@ -172,7 +175,7 @@ class EntityRepository ...@@ -172,7 +175,7 @@ class EntityRepository
if ($this->_class->hasField($fieldName)) { if ($this->_class->hasField($fieldName)) {
return $this->$method(array($fieldName => $arguments[0])); return $this->$method(array($fieldName => $arguments[0]));
} else { } else {
throw \Doctrine\Common\DoctrineException::invalidFindBy($by); throw ORMException::invalidFindByCall($this->_entityName, $fieldName, $method.$by);
} }
} }
} }
\ No newline at end of file
...@@ -4,7 +4,7 @@ namespace Doctrine\ORM; ...@@ -4,7 +4,7 @@ namespace Doctrine\ORM;
/** /**
* Base exception class for all ORM exceptions. * Base exception class for all ORM exceptions.
* *
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
* @since 2.0 * @since 2.0
*/ */
...@@ -14,46 +14,57 @@ class ORMException extends \Exception ...@@ -14,46 +14,57 @@ class ORMException extends \Exception
{ {
return new self("Entity of type " . get_class($entity) . " is missing an assigned ID."); return new self("Entity of type " . get_class($entity) . " is missing an assigned ID.");
} }
public static function unrecognizedField($field) public static function unrecognizedField($field)
{ {
return new self("Unrecognized field: $field"); return new self("Unrecognized field: $field");
} }
public static function removedEntityInCollectionDetected($entity, $assoc) public static function removedEntityInCollectionDetected($entity, $assoc)
{ {
return new self("Removed entity of type " . get_class($entity) return new self("Removed entity of type " . get_class($entity)
. " detected in collection '" . $assoc->sourceFieldName . "' during flush." . " detected in collection '" . $assoc->sourceFieldName . "' during flush."
. " Remove deleted entities from collections."); . " Remove deleted entities from collections.");
} }
public static function invalidEntityState($state) public static function invalidEntityState($state)
{ {
return new self("Invalid entity state: $state."); return new self("Invalid entity state: $state.");
} }
public static function detachedEntityCannotBeRemoved() public static function detachedEntityCannotBeRemoved()
{ {
return new self("A detached entity can not be removed."); return new self("A detached entity can not be removed.");
} }
public static function invalidFlushMode($mode) public static function invalidFlushMode($mode)
{ {
return new self("'$mode' is an invalid flush mode."); return new self("'$mode' is an invalid flush mode.");
} }
public static function entityManagerClosed() public static function entityManagerClosed()
{ {
return new self("The EntityManager is closed."); return new self("The EntityManager is closed.");
} }
public static function invalidHydrationMode($mode) public static function invalidHydrationMode($mode)
{ {
return new self("'$mode' is an invalid hydration mode."); return new self("'$mode' is an invalid hydration mode.");
} }
public static function mismatchedEventManager() public static function mismatchedEventManager()
{ {
return new self("Cannot use different EventManager instances for EntityManager and Connection."); 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"
);
}
} }
...@@ -25,13 +25,13 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase ...@@ -25,13 +25,13 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase
$user->username = 'romanb'; $user->username = 'romanb';
$user->status = 'freak'; $user->status = 'freak';
$this->_em->persist($user); $this->_em->persist($user);
$user2 = new CmsUser; $user2 = new CmsUser;
$user2->name = 'Guilherme'; $user2->name = 'Guilherme';
$user2->username = 'gblanco'; $user2->username = 'gblanco';
$user2->status = 'dev'; $user2->status = 'dev';
$this->_em->persist($user2); $this->_em->persist($user2);
$this->_em->flush(); $this->_em->flush();
$user1Id = $user->getId(); $user1Id = $user->getId();
unset($user); unset($user);
...@@ -39,32 +39,48 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase ...@@ -39,32 +39,48 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->_em->clear(); $this->_em->clear();
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
$user = $repos->find($user1Id); $user = $repos->find($user1Id);
$this->assertTrue($user instanceof CmsUser); $this->assertTrue($user instanceof CmsUser);
$this->assertEquals('Roman', $user->name); $this->assertEquals('Roman', $user->name);
$this->assertEquals('freak', $user->status); $this->assertEquals('freak', $user->status);
$this->_em->clear(); $this->_em->clear();
$users = $repos->findBy(array('status' => 'dev')); $users = $repos->findBy(array('status' => 'dev'));
$this->assertEquals(1, count($users)); $this->assertEquals(1, count($users));
$this->assertTrue($users[0] instanceof CmsUser); $this->assertTrue($users[0] instanceof CmsUser);
$this->assertEquals('Guilherme', $users[0]->name); $this->assertEquals('Guilherme', $users[0]->name);
$this->assertEquals('dev', $users[0]->status); $this->assertEquals('dev', $users[0]->status);
$this->_em->clear(); $this->_em->clear();
$users = $repos->findByStatus('dev'); $users = $repos->findByStatus('dev');
$this->assertEquals(1, count($users)); $this->assertEquals(1, count($users));
$this->assertTrue($users[0] instanceof CmsUser); $this->assertTrue($users[0] instanceof CmsUser);
$this->assertEquals('Guilherme', $users[0]->name); $this->assertEquals('Guilherme', $users[0]->name);
$this->assertEquals('dev', $users[0]->status); $this->assertEquals('dev', $users[0]->status);
$this->_em->clear(); $this->_em->clear();
$users = $repos->findAll(); $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');
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment