1
0
mirror of synced 2025-02-07 15:59:27 +03:00

Added count cache when lazy collection is not yet initialized. Some cosmetic changes (primarily, there's no ELSE).

This commit is contained in:
Guilherme Blanco 2014-05-16 04:22:11 +00:00
parent 92a2b01c77
commit d30e3ab43c
4 changed files with 29 additions and 16 deletions

View File

@ -28,8 +28,12 @@ use Doctrine\ORM\Persisters\EntityPersister;
/** /**
* A lazy collection that allow a fast count when using criteria object * A lazy collection that allow a fast count when using criteria object
* Once count gets executed once without collection being initialized, result
* is cached and returned on subsequent calls until collection gets loaded,
* then returning the number of loaded results.
* *
* @since 2.5 * @since 2.5
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Michaël Gallego <mic.gallego@gmail.com> * @author Michaël Gallego <mic.gallego@gmail.com>
*/ */
class LazyCriteriaCollection extends AbstractLazyCollection implements Selectable class LazyCriteriaCollection extends AbstractLazyCollection implements Selectable
@ -44,6 +48,11 @@ class LazyCriteriaCollection extends AbstractLazyCollection implements Selectabl
*/ */
protected $criteria; protected $criteria;
/**
* @var integer
*/
private $count;
/** /**
* @param EntityPersister $entityPersister * @param EntityPersister $entityPersister
* @param Criteria $criteria * @param Criteria $criteria
@ -57,7 +66,7 @@ class LazyCriteriaCollection extends AbstractLazyCollection implements Selectabl
/** /**
* Do an efficient count on the collection * Do an efficient count on the collection
* *
* @return int * @return integer
*/ */
public function count() public function count()
{ {
@ -65,7 +74,12 @@ class LazyCriteriaCollection extends AbstractLazyCollection implements Selectabl
return $this->collection->count(); return $this->collection->count();
} }
return $this->entityPersister->count($this->criteria); // Return cached result in case count query was already executed
if ($this->count) {
return $this->count;
}
return $this->count = $this->entityPersister->count($this->criteria);
} }
/** /**
@ -74,6 +88,7 @@ class LazyCriteriaCollection extends AbstractLazyCollection implements Selectabl
public function matching(Criteria $criteria) public function matching(Criteria $criteria)
{ {
$this->initialize(); $this->initialize();
return $this->collection->matching($criteria); return $this->collection->matching($criteria);
} }

View File

@ -883,10 +883,8 @@ final class PersistentCollection implements Collection, Selectable
$persister = $this->em->getUnitOfWork()->getEntityPersister($this->association['targetEntity']); $persister = $this->em->getUnitOfWork()->getEntityPersister($this->association['targetEntity']);
if ($this->association['fetch'] === ClassMetadataInfo::FETCH_EXTRA_LAZY) { return ($this->association['fetch'] === ClassMetadataInfo::FETCH_EXTRA_LAZY)
return new LazyCriteriaCollection($persister, $criteria); ? new LazyCriteriaCollection($persister, $criteria)
} else { : new ArrayCollection($persister->loadCriteria($criteria));
return new ArrayCollection($persister->loadCriteria($criteria));
}
} }
} }

View File

@ -848,9 +848,11 @@ class BasicEntityPersister implements EntityPersister
public function expandCriteriaParameters(Criteria $criteria) public function expandCriteriaParameters(Criteria $criteria)
{ {
$expression = $criteria->getWhereExpression(); $expression = $criteria->getWhereExpression();
$sqlParams = array();
$sqlTypes = array();
if ($expression === null) { if ($expression === null) {
return array(array(), array()); return array($sqlParams, $sqlTypes);
} }
$valueVisitor = new SqlValueVisitor(); $valueVisitor = new SqlValueVisitor();
@ -859,12 +861,10 @@ class BasicEntityPersister implements EntityPersister
list($params, $types) = $valueVisitor->getParamsAndTypes(); list($params, $types) = $valueVisitor->getParamsAndTypes();
$sqlParams = array();
foreach ($params as $param) { foreach ($params as $param) {
$sqlParams[] = $this->getValue($param); $sqlParams[] = $this->getValue($param);
} }
$sqlTypes = array();
foreach ($types as $type) { foreach ($types as $type) {
list($field, $value) = $type; list($field, $value) = $type;
$sqlTypes[] = $this->getType($field, $value); $sqlTypes[] = $this->getType($field, $value);