Allowed to get count query from paginator.
This commit is contained in:
parent
705c33bc35
commit
ae198d5e45
@ -118,38 +118,8 @@ class Paginator implements \Countable, \IteratorAggregate
|
|||||||
public function count()
|
public function count()
|
||||||
{
|
{
|
||||||
if ($this->count === null) {
|
if ($this->count === null) {
|
||||||
/* @var $countQuery Query */
|
$countQuery = $this->getCountQuery();
|
||||||
$countQuery = $this->cloneQuery($this->query);
|
|
||||||
|
|
||||||
if ( ! $countQuery->hasHint(CountWalker::HINT_DISTINCT)) {
|
|
||||||
$countQuery->setHint(CountWalker::HINT_DISTINCT, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->useOutputWalker($countQuery)) {
|
|
||||||
$platform = $countQuery->getEntityManager()->getConnection()->getDatabasePlatform(); // law of demeter win
|
|
||||||
|
|
||||||
$rsm = new ResultSetMapping();
|
|
||||||
$rsm->addScalarResult($platform->getSQLResultCasing('dctrn_count'), 'count');
|
|
||||||
|
|
||||||
$countQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\CountOutputWalker');
|
|
||||||
$countQuery->setResultSetMapping($rsm);
|
|
||||||
} else {
|
|
||||||
$this->appendTreeWalker($countQuery, 'Doctrine\ORM\Tools\Pagination\CountWalker');
|
|
||||||
}
|
|
||||||
|
|
||||||
$countQuery->setFirstResult(null)->setMaxResults(null);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$parser = new Query\Parser($countQuery);
|
|
||||||
$parameterMappings = $parser->parse($parser)->getParameterMappings();
|
|
||||||
$parameters = $countQuery->getParameters();
|
|
||||||
foreach($parameters as $k=>$param){
|
|
||||||
if(!array_key_exists($param->getName(), $parameterMappings)){
|
|
||||||
$parameters->remove($k);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$countQuery->setParameters($parameters);
|
|
||||||
|
|
||||||
$data = $countQuery->getScalarResult();
|
$data = $countQuery->getScalarResult();
|
||||||
$data = array_map('current', $data);
|
$data = array_map('current', $data);
|
||||||
$this->count = array_sum($data);
|
$this->count = array_sum($data);
|
||||||
@ -161,6 +131,39 @@ class Paginator implements \Countable, \IteratorAggregate
|
|||||||
return $this->count;
|
return $this->count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getCountQuery(){
|
||||||
|
/* @var $countQuery Query */
|
||||||
|
$countQuery = $this->cloneQuery($this->query);
|
||||||
|
|
||||||
|
if ( ! $countQuery->hasHint(CountWalker::HINT_DISTINCT)) {
|
||||||
|
$countQuery->setHint(CountWalker::HINT_DISTINCT, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->useOutputWalker($countQuery)) {
|
||||||
|
$platform = $countQuery->getEntityManager()->getConnection()->getDatabasePlatform(); // law of demeter win
|
||||||
|
|
||||||
|
$rsm = new ResultSetMapping();
|
||||||
|
$rsm->addScalarResult($platform->getSQLResultCasing('dctrn_count'), 'count');
|
||||||
|
|
||||||
|
$countQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\CountOutputWalker');
|
||||||
|
$countQuery->setResultSetMapping($rsm);
|
||||||
|
} else {
|
||||||
|
$this->appendTreeWalker($countQuery, 'Doctrine\ORM\Tools\Pagination\CountWalker');
|
||||||
|
}
|
||||||
|
|
||||||
|
$countQuery->setFirstResult(null)->setMaxResults(null);
|
||||||
|
$parser = new Query\Parser($countQuery);
|
||||||
|
$parameterMappings = $parser->parse($parser)->getParameterMappings();
|
||||||
|
$parameters = $countQuery->getParameters();
|
||||||
|
foreach($parameters as $k=>$param){
|
||||||
|
if(!array_key_exists($param->getName(), $parameterMappings)){
|
||||||
|
$parameters->remove($k);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$countQuery->setParameters($parameters);
|
||||||
|
return $countQuery;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user