1
0
mirror of synced 2025-02-09 08:49:26 +03:00

Expose EntityPersister::count() through EntityRepository::count()

This commit is contained in:
Javier Spagnoletti 2016-09-02 15:43:28 -03:00 committed by Marco Pivetta
parent 35341769ea
commit a90035e81a
5 changed files with 121 additions and 56 deletions

View File

@ -727,6 +727,14 @@ examples are equivalent:
// 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
<?php
// Check there is no user with nickname
$availableNickname = 0 === $em->getRepository('MyProject\Domain\User')->count(array('nickname' => 'nonexistent'));
By Criteria
~~~~~~~~~~~

View File

@ -1535,6 +1535,16 @@ As an example here is the code of the first use case "List of Bugs":
Using EntityRepositories you can avoid coupling your model with specific query logic.
You can also re-use query logic easily throughout your application.
The method ``count()`` takes an array of fields or association keys and the values to match against.
This provides you with a convenient and lightweight way to count a resultset when you don't need to
deal with it:
.. code-block:: php
<?php
$productCount = $entityManager->getRepository('Product')
->count(array('name' => $productName));
Conclusion
----------

View File

@ -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]));

View File

@ -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();
}
/**

View File

@ -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
*/