Added count cache when lazy collection is not yet initialized. Some cosmetic changes (primarily, there's no ELSE).
This commit is contained in:
parent
92a2b01c77
commit
d30e3ab43c
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
@ -560,15 +560,15 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister
|
|||||||
*/
|
*/
|
||||||
private function getJoinSql($baseTableAlias)
|
private function getJoinSql($baseTableAlias)
|
||||||
{
|
{
|
||||||
$joinSql = '';
|
$joinSql = '';
|
||||||
$identifierColumn = $this->class->getIdentifierColumnNames();
|
$identifierColumn = $this->class->getIdentifierColumnNames();
|
||||||
|
|
||||||
// INNER JOIN parent tables
|
// INNER JOIN parent tables
|
||||||
foreach ($this->class->parentClasses as $parentClassName) {
|
foreach ($this->class->parentClasses as $parentClassName) {
|
||||||
$conditions = array();
|
$conditions = array();
|
||||||
$parentClass = $this->em->getClassMetadata($parentClassName);
|
$parentClass = $this->em->getClassMetadata($parentClassName);
|
||||||
$tableAlias = $this->getSQLTableAlias($parentClassName);
|
$tableAlias = $this->getSQLTableAlias($parentClassName);
|
||||||
$joinSql .= ' INNER JOIN ' . $this->quoteStrategy->getTableName($parentClass, $this->platform) . ' ' . $tableAlias . ' ON ';
|
$joinSql .= ' INNER JOIN ' . $this->quoteStrategy->getTableName($parentClass, $this->platform) . ' ' . $tableAlias . ' ON ';
|
||||||
|
|
||||||
|
|
||||||
foreach ($identifierColumn as $idColumn) {
|
foreach ($identifierColumn as $idColumn) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user