From a90035e81af2e9a424c0268a36101c3483e06571 Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Fri, 2 Sep 2016 15:43:28 -0300 Subject: [PATCH 01/11] Expose `EntityPersister::count()` through `EntityRepository::count()` --- docs/en/reference/working-with-objects.rst | 54 +++++++++------- docs/en/tutorials/getting-started.rst | 62 +++++++++++-------- lib/Doctrine/ORM/EntityRepository.php | 35 +++++++++-- .../Entity/BasicEntityPersister.php | 2 +- .../ORM/Functional/EntityRepositoryTest.php | 24 +++++++ 5 files changed, 121 insertions(+), 56 deletions(-) diff --git a/docs/en/reference/working-with-objects.rst b/docs/en/reference/working-with-objects.rst index 8368c9f13..bcc9bf185 100644 --- a/docs/en/reference/working-with-objects.rst +++ b/docs/en/reference/working-with-objects.rst @@ -41,7 +41,7 @@ headline "Hello World" with the ID 1234: find('CMS\Article', 1234); $article->setHeadline('Hello World dude!'); - + $article2 = $entityManager->find('CMS\Article', 1234); echo $article2->getHeadline(); @@ -93,25 +93,25 @@ from newly opened EntityManager. { /** @Id @Column(type="integer") @GeneratedValue */ private $id; - + /** @Column(type="string") */ private $headline; - + /** @ManyToOne(targetEntity="User") */ private $author; - + /** @OneToMany(targetEntity="Comment", mappedBy="article") */ private $comments; - + public function __construct() { $this->comments = new ArrayCollection(); } - + public function getAuthor() { return $this->author; } public function getComments() { return $this->comments; } } - + $article = $em->find('Article', 1); This code only retrieves the ``Article`` instance with id 1 executing @@ -132,22 +132,22 @@ your code. See the following code: find('Article', 1); - + // accessing a method of the user instance triggers the lazy-load echo "Author: " . $article->getAuthor()->getName() . "\n"; - + // Lazy Loading Proxies pass instanceof tests: if ($article->getAuthor() instanceof User) { // a User Proxy is a generated "UserProxy" class } - + // accessing the comments as an iterator triggers the lazy-load // retrieving ALL the comments of this article from the database // using a single SELECT statement foreach ($article->getComments() as $comment) { echo $comment->getText() . "\n\n"; } - + // Article::$comments passes instanceof tests for the Collection interface // But it will NOT pass for the ArrayCollection interface if ($article->getComments() instanceof \Doctrine\Common\Collections\Collection) { @@ -167,7 +167,7 @@ methods along the lines of the ``getName()`` method shown below: { // lazy loading code } - + public function getName() { $this->_load(); @@ -262,7 +262,7 @@ which means that its persistent state will be deleted once for and appear in query and collection results. See the section on :ref:`Database and UnitOfWork Out-Of-Sync ` for more information. - + Example: @@ -681,13 +681,13 @@ methods on a repository as follows: getRepository('MyProject\Domain\User')->findBy(array('age' => 20)); - + // All users that are 20 years old and have a surname of 'Miller' $users = $em->getRepository('MyProject\Domain\User')->findBy(array('age' => 20, 'surname' => 'Miller')); - + // A single user by its nickname $user = $em->getRepository('MyProject\Domain\User')->findOneBy(array('nickname' => 'romanb')); @@ -723,10 +723,18 @@ examples are equivalent: getRepository('MyProject\Domain\User')->findOneBy(array('nickname' => 'romanb')); - + // A single user by its nickname (__call magic) $user = $em->getRepository('MyProject\Domain\User')->findOneByNickname('romanb'); +Additionally, you can just count the result of the provided conditions when you don't really need the data: + +.. code-block:: php + + getRepository('MyProject\Domain\User')->count(array('nickname' => 'nonexistent')); + By Criteria ~~~~~~~~~~~ @@ -774,7 +782,7 @@ A DQL query is represented by an instance of the createQuery("select u from MyDomain\Model\User u where u.age >= 20 and u.age <= 30"); $users = $q->getResult(); @@ -817,18 +825,18 @@ in a central location. getRepository('MyDomain\Model\User')->getAllAdminUsers(); diff --git a/docs/en/tutorials/getting-started.rst b/docs/en/tutorials/getting-started.rst index ef4734317..5e0444246 100644 --- a/docs/en/tutorials/getting-started.rst +++ b/docs/en/tutorials/getting-started.rst @@ -103,7 +103,7 @@ Install Doctrine using the Composer Dependency Management tool, by calling: $ composer install This will install the packages Doctrine Common, Doctrine DBAL, Doctrine ORM, -Symfony YAML and Symfony Console into the `vendor` directory. The Symfony +Symfony YAML and Symfony Console into the `vendor` directory. The Symfony dependencies are not required by Doctrine but will be used in this tutorial. Add the following directories: @@ -131,22 +131,22 @@ step: // bootstrap.php use Doctrine\ORM\Tools\Setup; use Doctrine\ORM\EntityManager; - + require_once "vendor/autoload.php"; - + // Create a simple "default" Doctrine ORM configuration for Annotations $isDevMode = true; $config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src"), $isDevMode); // or if you prefer yaml or XML //$config = Setup::createXMLMetadataConfiguration(array(__DIR__."/config/xml"), $isDevMode); //$config = Setup::createYAMLMetadataConfiguration(array(__DIR__."/config/yaml"), $isDevMode); - + // database configuration parameters $conn = array( 'driver' => 'pdo_sqlite', 'path' => __DIR__ . '/db.sqlite', ); - + // obtaining the entity manager $entityManager = EntityManager::create($conn, $config); @@ -185,7 +185,7 @@ doctrine command. Its a fairly simple file: getRepository('Product') + ->count(array('name' => $productName)); + Conclusion ---------- diff --git a/lib/Doctrine/ORM/EntityRepository.php b/lib/Doctrine/ORM/EntityRepository.php index 08b2ff193..2b8c1cb51 100644 --- a/lib/Doctrine/ORM/EntityRepository.php +++ b/lib/Doctrine/ORM/EntityRepository.php @@ -196,16 +196,32 @@ class EntityRepository implements ObjectRepository, Selectable } /** - * Adds support for magic finders. + * Counts entities by a set of criteria. + * + * @todo Add this method to `ObjectRepository` interface in the next major release + * + * @param array $criteria + * + * @return int The quantity of objects that matches the criteria. + */ + public function count(array $criteria) + { + $persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName); + + return $persister->count($criteria); + } + + /** + * Adds support for magic finders/counters. * * @param string $method * @param array $arguments * - * @return array|object The found entity/entities. + * @return array|object|int The found entity/entities or its resulting count. * * @throws ORMException - * @throws \BadMethodCallException If the method called is an invalid find* method - * or no find* method at all and therefore an invalid + * @throws \BadMethodCallException If the method called is an invalid find* or countBy method + * or no find* or countBy method at all and therefore an invalid * method call. */ public function __call($method, $arguments) @@ -221,6 +237,11 @@ class EntityRepository implements ObjectRepository, Selectable $method = 'findOneBy'; break; + case (0 === strpos($method, 'countBy')): + $by = substr($method, 7); + $method = 'count'; + break; + default: throw new \BadMethodCallException( "Undefined method '$method'. The method name must start with ". @@ -228,14 +249,16 @@ class EntityRepository implements ObjectRepository, Selectable ); } - if (empty($arguments)) { + $argsCount = count($arguments); + + if (0 === $argsCount) { throw ORMException::findByRequiresParameter($method . $by); } $fieldName = lcfirst(\Doctrine\Common\Util\Inflector::classify($by)); if ($this->_class->hasField($fieldName) || $this->_class->hasAssociation($fieldName)) { - switch (count($arguments)) { + switch ($argsCount) { case 1: return $this->$method(array($fieldName => $arguments[0])); diff --git a/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php index 3b2faf878..c62a10a12 100644 --- a/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php @@ -822,7 +822,7 @@ class BasicEntityPersister implements EntityPersister ? $this->expandCriteriaParameters($criteria) : $this->expandParameters($criteria); - return $this->conn->executeQuery($sql, $params, $types)->fetchColumn(); + return (int) $this->conn->executeQuery($sql, $params, $types)->fetchColumn(); } /** diff --git a/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php b/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php index ffca89ba0..e0b96387a 100644 --- a/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php @@ -272,6 +272,30 @@ class EntityRepositoryTest extends OrmFunctionalTestCase $this->assertEquals(4, count($users)); } + public function testCount() + { + $this->loadFixture(); + $repos = $this->_em->getRepository(CmsUser::class); + + $userCount = $repos->count(array()); + $this->assertSame(4, $userCount); + + $userCount = $repos->count(array('status' => 'dev')); + $this->assertSame(2, $userCount); + + $userCount = $repos->count(array('status' => 'nonexistent')); + $this->assertSame(0, $userCount); + } + + public function testCountBy() + { + $this->loadFixture(); + $repos = $this->_em->getRepository(CmsUser::class); + + $userCount = $repos->countByStatus('dev'); + $this->assertSame(2, $userCount); + } + /** * @expectedException \Doctrine\ORM\ORMException */ From bea3c653bc3e0780ca6ec170235953ef2a75842e Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Wed, 7 Sep 2016 17:39:51 -0300 Subject: [PATCH 02/11] Updated docs --- docs/en/reference/working-with-objects.rst | 48 +++++++++---------- docs/en/tutorials/getting-started.rst | 56 +++++++++++----------- 2 files changed, 52 insertions(+), 52 deletions(-) diff --git a/docs/en/reference/working-with-objects.rst b/docs/en/reference/working-with-objects.rst index bcc9bf185..2b52fac41 100644 --- a/docs/en/reference/working-with-objects.rst +++ b/docs/en/reference/working-with-objects.rst @@ -41,7 +41,7 @@ headline "Hello World" with the ID 1234: find('CMS\Article', 1234); $article->setHeadline('Hello World dude!'); - + $article2 = $entityManager->find('CMS\Article', 1234); echo $article2->getHeadline(); @@ -93,25 +93,25 @@ from newly opened EntityManager. { /** @Id @Column(type="integer") @GeneratedValue */ private $id; - + /** @Column(type="string") */ private $headline; - + /** @ManyToOne(targetEntity="User") */ private $author; - + /** @OneToMany(targetEntity="Comment", mappedBy="article") */ private $comments; - + public function __construct() { $this->comments = new ArrayCollection(); } - + public function getAuthor() { return $this->author; } public function getComments() { return $this->comments; } } - + $article = $em->find('Article', 1); This code only retrieves the ``Article`` instance with id 1 executing @@ -132,22 +132,22 @@ your code. See the following code: find('Article', 1); - + // accessing a method of the user instance triggers the lazy-load echo "Author: " . $article->getAuthor()->getName() . "\n"; - + // Lazy Loading Proxies pass instanceof tests: if ($article->getAuthor() instanceof User) { // a User Proxy is a generated "UserProxy" class } - + // accessing the comments as an iterator triggers the lazy-load // retrieving ALL the comments of this article from the database // using a single SELECT statement foreach ($article->getComments() as $comment) { echo $comment->getText() . "\n\n"; } - + // Article::$comments passes instanceof tests for the Collection interface // But it will NOT pass for the ArrayCollection interface if ($article->getComments() instanceof \Doctrine\Common\Collections\Collection) { @@ -167,7 +167,7 @@ methods along the lines of the ``getName()`` method shown below: { // lazy loading code } - + public function getName() { $this->_load(); @@ -262,7 +262,7 @@ which means that its persistent state will be deleted once for and appear in query and collection results. See the section on :ref:`Database and UnitOfWork Out-Of-Sync ` for more information. - + Example: @@ -681,13 +681,13 @@ methods on a repository as follows: getRepository('MyProject\Domain\User')->findBy(array('age' => 20)); - + // All users that are 20 years old and have a surname of 'Miller' $users = $em->getRepository('MyProject\Domain\User')->findBy(array('age' => 20, 'surname' => 'Miller')); - + // A single user by its nickname $user = $em->getRepository('MyProject\Domain\User')->findOneBy(array('nickname' => 'romanb')); @@ -723,7 +723,7 @@ examples are equivalent: getRepository('MyProject\Domain\User')->findOneBy(array('nickname' => 'romanb')); - + // A single user by its nickname (__call magic) $user = $em->getRepository('MyProject\Domain\User')->findOneByNickname('romanb'); @@ -733,7 +733,7 @@ Additionally, you can just count the result of the provided conditions when you getRepository('MyProject\Domain\User')->count(array('nickname' => 'nonexistent')); + $availableNickname = 0 === $em->getRepository('MyProject\Domain\User')->count(['nickname' => 'nonexistent']); By Criteria ~~~~~~~~~~~ @@ -782,7 +782,7 @@ A DQL query is represented by an instance of the createQuery("select u from MyDomain\Model\User u where u.age >= 20 and u.age <= 30"); $users = $q->getResult(); @@ -825,18 +825,18 @@ in a central location. getRepository('MyDomain\Model\User')->getAllAdminUsers(); diff --git a/docs/en/tutorials/getting-started.rst b/docs/en/tutorials/getting-started.rst index 5e0444246..05492b133 100644 --- a/docs/en/tutorials/getting-started.rst +++ b/docs/en/tutorials/getting-started.rst @@ -103,7 +103,7 @@ Install Doctrine using the Composer Dependency Management tool, by calling: $ composer install This will install the packages Doctrine Common, Doctrine DBAL, Doctrine ORM, -Symfony YAML and Symfony Console into the `vendor` directory. The Symfony +Symfony YAML and Symfony Console into the `vendor` directory. The Symfony dependencies are not required by Doctrine but will be used in this tutorial. Add the following directories: @@ -131,22 +131,22 @@ step: // bootstrap.php use Doctrine\ORM\Tools\Setup; use Doctrine\ORM\EntityManager; - + require_once "vendor/autoload.php"; - + // Create a simple "default" Doctrine ORM configuration for Annotations $isDevMode = true; $config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src"), $isDevMode); // or if you prefer yaml or XML //$config = Setup::createXMLMetadataConfiguration(array(__DIR__."/config/xml"), $isDevMode); //$config = Setup::createYAMLMetadataConfiguration(array(__DIR__."/config/yaml"), $isDevMode); - + // database configuration parameters $conn = array( 'driver' => 'pdo_sqlite', 'path' => __DIR__ . '/db.sqlite', ); - + // obtaining the entity manager $entityManager = EntityManager::create($conn, $config); @@ -185,7 +185,7 @@ doctrine command. Its a fairly simple file: getRepository('Product') - ->count(array('name' => $productName)); + $productCount = $entityManager->getRepository(Product::class) + ->count(['name' => $productName]); Conclusion ---------- From 591bae0855c111d1c247eb204125643566b30868 Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Wed, 7 Sep 2016 18:43:17 -0300 Subject: [PATCH 03/11] Swap logic from `EntityRespository::__call()` --- lib/Doctrine/ORM/EntityRepository.php | 106 ++++++++++++++------------ lib/Doctrine/ORM/ORMException.php | 15 ++++ 2 files changed, 71 insertions(+), 50 deletions(-) diff --git a/lib/Doctrine/ORM/EntityRepository.php b/lib/Doctrine/ORM/EntityRepository.php index 2b8c1cb51..39f8bf3bc 100644 --- a/lib/Doctrine/ORM/EntityRepository.php +++ b/lib/Doctrine/ORM/EntityRepository.php @@ -212,71 +212,34 @@ class EntityRepository implements ObjectRepository, Selectable } /** - * Adds support for magic finders/counters. + * Adds support for magic method calls. * * @param string $method * @param array $arguments * - * @return array|object|int The found entity/entities or its resulting count. + * @return mixed The returned value from the resolved method. * * @throws ORMException - * @throws \BadMethodCallException If the method called is an invalid find* or countBy method - * or no find* or countBy method at all and therefore an invalid - * method call. + * @throws \BadMethodCallException If the method called is invalid */ public function __call($method, $arguments) { - switch (true) { - case (0 === strpos($method, 'findBy')): - $by = substr($method, 6); - $method = 'findBy'; - break; - - case (0 === strpos($method, 'findOneBy')): - $by = substr($method, 9); - $method = 'findOneBy'; - break; - - case (0 === strpos($method, 'countBy')): - $by = substr($method, 7); - $method = 'count'; - break; - - default: - throw new \BadMethodCallException( - "Undefined method '$method'. The method name must start with ". - "either findBy or findOneBy!" - ); + if (0 === strpos($method, 'findBy')) { + return $this->resolveMagicCall('findBy', substr($method, 6), $arguments); } - $argsCount = count($arguments); - - if (0 === $argsCount) { - throw ORMException::findByRequiresParameter($method . $by); + if (0 === strpos($method, 'findOneBy')) { + return $this->resolveMagicCall('findOneBy', substr($method, 9), $arguments); } - $fieldName = lcfirst(\Doctrine\Common\Util\Inflector::classify($by)); - - if ($this->_class->hasField($fieldName) || $this->_class->hasAssociation($fieldName)) { - switch ($argsCount) { - case 1: - return $this->$method(array($fieldName => $arguments[0])); - - case 2: - return $this->$method(array($fieldName => $arguments[0]), $arguments[1]); - - case 3: - return $this->$method(array($fieldName => $arguments[0]), $arguments[1], $arguments[2]); - - case 4: - return $this->$method(array($fieldName => $arguments[0]), $arguments[1], $arguments[2], $arguments[3]); - - default: - // Do nothing - } + if (0 === strpos($method, 'countBy')) { + return $this->resolveMagicCall('count', substr($method, 7), $arguments); } - throw ORMException::invalidFindByCall($this->_entityName, $fieldName, $method.$by); + throw new \BadMethodCallException( + "Undefined method '$method'. The method name must start with ". + "either findBy or findOneBy!" + ); } /** @@ -325,4 +288,47 @@ class EntityRepository implements ObjectRepository, Selectable return new LazyCriteriaCollection($persister, $criteria); } + + /** + * Resolves a magic method call to the proper existent method at `EntityRepository`. + * + * @param string $method The method to call + * @param string $by The property name used as condition + * @param array $arguments The arguments to pass at method call + * + * @throws ORMException If the method called is invalid. + * + * @return mixed + */ + private function resolveMagicCall($method, $by, array $arguments = []) + { + $argsCount = count($arguments); + + if (0 === $argsCount) { + throw ORMException::findByRequiresParameter($method . $by); + } + + $fieldName = lcfirst(\Doctrine\Common\Util\Inflector::classify($by)); + + if ($this->_class->hasField($fieldName) || $this->_class->hasAssociation($fieldName)) { + switch ($argsCount) { + case 1: + return $this->$method(array($fieldName => $arguments[0])); + + case 2: + return $this->$method(array($fieldName => $arguments[0]), $arguments[1]); + + case 3: + return $this->$method(array($fieldName => $arguments[0]), $arguments[1], $arguments[2]); + + case 4: + return $this->$method(array($fieldName => $arguments[0]), $arguments[1], $arguments[2], $arguments[3]); + + default: + // Do nothing + } + } + + throw ORMException::invalidMagicCall($this->_entityName, $fieldName, $method.$by); + } } diff --git a/lib/Doctrine/ORM/ORMException.php b/lib/Doctrine/ORM/ORMException.php index 6d8a6d631..919789d92 100644 --- a/lib/Doctrine/ORM/ORMException.php +++ b/lib/Doctrine/ORM/ORMException.php @@ -187,6 +187,21 @@ class ORMException extends Exception ); } + /** + * @param string $entityName + * @param string $fieldName + * @param string $method + * + * @return ORMException + */ + public static function invalidMagicCall($entityName, $fieldName, $method) + { + return new self( + "Entity '".$entityName."' has no field '".$fieldName."'. ". + "You can therefore not call '".$method."' on the entities' repository" + ); + } + /** * @param string $entityName * @param string $associationFieldName From 61f6b667c0026565f5cbfa9f0c4066266bdb61e9 Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Wed, 7 Sep 2016 19:24:38 -0300 Subject: [PATCH 04/11] Remove `default` clause at `EntityRepository::resolveMagicCall()` --- lib/Doctrine/ORM/EntityRepository.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/Doctrine/ORM/EntityRepository.php b/lib/Doctrine/ORM/EntityRepository.php index 39f8bf3bc..00e53c341 100644 --- a/lib/Doctrine/ORM/EntityRepository.php +++ b/lib/Doctrine/ORM/EntityRepository.php @@ -323,9 +323,6 @@ class EntityRepository implements ObjectRepository, Selectable case 4: return $this->$method(array($fieldName => $arguments[0]), $arguments[1], $arguments[2], $arguments[3]); - - default: - // Do nothing } } From 6f79a378d56101516ad379307794e4ada0a76caf Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 8 Sep 2016 00:43:29 +0200 Subject: [PATCH 05/11] #6003 removed useless method parameter count checking duplication --- lib/Doctrine/ORM/EntityRepository.php | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/lib/Doctrine/ORM/EntityRepository.php b/lib/Doctrine/ORM/EntityRepository.php index 00e53c341..a3fcf0aff 100644 --- a/lib/Doctrine/ORM/EntityRepository.php +++ b/lib/Doctrine/ORM/EntityRepository.php @@ -310,22 +310,10 @@ class EntityRepository implements ObjectRepository, Selectable $fieldName = lcfirst(\Doctrine\Common\Util\Inflector::classify($by)); - if ($this->_class->hasField($fieldName) || $this->_class->hasAssociation($fieldName)) { - switch ($argsCount) { - case 1: - return $this->$method(array($fieldName => $arguments[0])); - - case 2: - return $this->$method(array($fieldName => $arguments[0]), $arguments[1]); - - case 3: - return $this->$method(array($fieldName => $arguments[0]), $arguments[1], $arguments[2]); - - case 4: - return $this->$method(array($fieldName => $arguments[0]), $arguments[1], $arguments[2], $arguments[3]); - } + if (! ($this->_class->hasField($fieldName) || $this->_class->hasAssociation($fieldName))) { + throw ORMException::invalidMagicCall($this->_entityName, $fieldName, $method . $by); } - throw ORMException::invalidMagicCall($this->_entityName, $fieldName, $method.$by); + return $this->$method([$fieldName => $arguments[0]], ...array_slice($arguments, 1)); } } From 5e51a985b7df08f45604642e154a98c65b9aac31 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 8 Sep 2016 00:47:39 +0200 Subject: [PATCH 06/11] #6003 no default parameter needed --- lib/Doctrine/ORM/EntityRepository.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/EntityRepository.php b/lib/Doctrine/ORM/EntityRepository.php index a3fcf0aff..7ba60b71e 100644 --- a/lib/Doctrine/ORM/EntityRepository.php +++ b/lib/Doctrine/ORM/EntityRepository.php @@ -300,7 +300,7 @@ class EntityRepository implements ObjectRepository, Selectable * * @return mixed */ - private function resolveMagicCall($method, $by, array $arguments = []) + private function resolveMagicCall($method, $by, array $arguments) { $argsCount = count($arguments); From de4c854ac955526285eab6111b9ba9a446d44b62 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 8 Sep 2016 00:48:52 +0200 Subject: [PATCH 07/11] #6003 removed useless `count()` call --- lib/Doctrine/ORM/EntityRepository.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/Doctrine/ORM/EntityRepository.php b/lib/Doctrine/ORM/EntityRepository.php index 7ba60b71e..cdd90baf0 100644 --- a/lib/Doctrine/ORM/EntityRepository.php +++ b/lib/Doctrine/ORM/EntityRepository.php @@ -302,9 +302,7 @@ class EntityRepository implements ObjectRepository, Selectable */ private function resolveMagicCall($method, $by, array $arguments) { - $argsCount = count($arguments); - - if (0 === $argsCount) { + if (! $arguments) { throw ORMException::findByRequiresParameter($method . $by); } From 7bf4a65c92413773af3039448eccc296c34cffe9 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 8 Sep 2016 00:53:35 +0200 Subject: [PATCH 08/11] #6003 imported used symbol --- lib/Doctrine/ORM/EntityRepository.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/EntityRepository.php b/lib/Doctrine/ORM/EntityRepository.php index cdd90baf0..ce7b7c23a 100644 --- a/lib/Doctrine/ORM/EntityRepository.php +++ b/lib/Doctrine/ORM/EntityRepository.php @@ -19,6 +19,7 @@ namespace Doctrine\ORM; +use Doctrine\Common\Util\Inflector; use Doctrine\ORM\Query\ResultSetMappingBuilder; use Doctrine\Common\Persistence\ObjectRepository; use Doctrine\Common\Collections\Selectable; @@ -306,7 +307,7 @@ class EntityRepository implements ObjectRepository, Selectable throw ORMException::findByRequiresParameter($method . $by); } - $fieldName = lcfirst(\Doctrine\Common\Util\Inflector::classify($by)); + $fieldName = lcfirst(Inflector::classify($by)); if (! ($this->_class->hasField($fieldName) || $this->_class->hasAssociation($fieldName))) { throw ORMException::invalidMagicCall($this->_entityName, $fieldName, $method . $by); From e2cba87662c0ce01fb8b680daa3c6857ee6dfa1c Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 8 Sep 2016 00:54:49 +0200 Subject: [PATCH 09/11] #6003 corrected return type definition --- lib/Doctrine/ORM/EntityRepository.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/EntityRepository.php b/lib/Doctrine/ORM/EntityRepository.php index ce7b7c23a..f47608ea7 100644 --- a/lib/Doctrine/ORM/EntityRepository.php +++ b/lib/Doctrine/ORM/EntityRepository.php @@ -203,7 +203,7 @@ class EntityRepository implements ObjectRepository, Selectable * * @param array $criteria * - * @return int The quantity of objects that matches the criteria. + * @return int The cardinality of the objects that match the given criteria. */ public function count(array $criteria) { From 36e99040824a3da5554ab38427406ec5b679f5a8 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 8 Sep 2016 00:55:09 +0200 Subject: [PATCH 10/11] #6003 inlined persister retrieval --- lib/Doctrine/ORM/EntityRepository.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/Doctrine/ORM/EntityRepository.php b/lib/Doctrine/ORM/EntityRepository.php index f47608ea7..85dd63c6e 100644 --- a/lib/Doctrine/ORM/EntityRepository.php +++ b/lib/Doctrine/ORM/EntityRepository.php @@ -207,9 +207,7 @@ class EntityRepository implements ObjectRepository, Selectable */ public function count(array $criteria) { - $persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName); - - return $persister->count($criteria); + return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->count($criteria); } /** From c5c56a9dad69d138057c1e1707390dec6c241912 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 8 Sep 2016 00:55:49 +0200 Subject: [PATCH 11/11] #6003 clarifying thrown exception --- lib/Doctrine/ORM/EntityRepository.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/EntityRepository.php b/lib/Doctrine/ORM/EntityRepository.php index 85dd63c6e..0c2823757 100644 --- a/lib/Doctrine/ORM/EntityRepository.php +++ b/lib/Doctrine/ORM/EntityRepository.php @@ -295,7 +295,7 @@ class EntityRepository implements ObjectRepository, Selectable * @param string $by The property name used as condition * @param array $arguments The arguments to pass at method call * - * @throws ORMException If the method called is invalid. + * @throws ORMException If the method called is invalid or the requested field/association does not exist * * @return mixed */