1
0
mirror of synced 2025-01-31 04:21:44 +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
* 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
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Michaël Gallego <mic.gallego@gmail.com>
*/
class LazyCriteriaCollection extends AbstractLazyCollection implements Selectable
@ -44,6 +48,11 @@ class LazyCriteriaCollection extends AbstractLazyCollection implements Selectabl
*/
protected $criteria;
/**
* @var integer
*/
private $count;
/**
* @param EntityPersister $entityPersister
* @param Criteria $criteria
@ -57,7 +66,7 @@ class LazyCriteriaCollection extends AbstractLazyCollection implements Selectabl
/**
* Do an efficient count on the collection
*
* @return int
* @return integer
*/
public function count()
{
@ -65,7 +74,12 @@ class LazyCriteriaCollection extends AbstractLazyCollection implements Selectabl
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)
{
$this->initialize();
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']);
if ($this->association['fetch'] === ClassMetadataInfo::FETCH_EXTRA_LAZY) {
return new LazyCriteriaCollection($persister, $criteria);
} else {
return new ArrayCollection($persister->loadCriteria($criteria));
}
return ($this->association['fetch'] === ClassMetadataInfo::FETCH_EXTRA_LAZY)
? new LazyCriteriaCollection($persister, $criteria)
: new ArrayCollection($persister->loadCriteria($criteria));
}
}

View File

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

View File

@ -560,15 +560,15 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister
*/
private function getJoinSql($baseTableAlias)
{
$joinSql = '';
$identifierColumn = $this->class->getIdentifierColumnNames();
$joinSql = '';
$identifierColumn = $this->class->getIdentifierColumnNames();
// INNER JOIN parent tables
foreach ($this->class->parentClasses as $parentClassName) {
$conditions = array();
$parentClass = $this->em->getClassMetadata($parentClassName);
$tableAlias = $this->getSQLTableAlias($parentClassName);
$joinSql .= ' INNER JOIN ' . $this->quoteStrategy->getTableName($parentClass, $this->platform) . ' ' . $tableAlias . ' ON ';
$conditions = array();
$parentClass = $this->em->getClassMetadata($parentClassName);
$tableAlias = $this->getSQLTableAlias($parentClassName);
$joinSql .= ' INNER JOIN ' . $this->quoteStrategy->getTableName($parentClass, $this->platform) . ' ' . $tableAlias . ' ON ';
foreach ($identifierColumn as $idColumn) {