Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed counting exception #1188

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 34 additions & 21 deletions lib/Doctrine/ORM/Tools/Pagination/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,27 +118,7 @@ public function setUseOutputWalkers($useOutputWalkers)
public function count()
{
if ($this->count === null) {
/* @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);

$countQuery = $this->getCountQuery();
try {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an empty newline before this line

$data = $countQuery->getScalarResult();
$data = array_map('current', $data);
Expand All @@ -151,6 +131,39 @@ public function count()
return $this->count;
}

public function getCountQuery(){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing docblocks: also, please put the brace on a new line

/* @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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parse() does not accept parameters

$parameters = $countQuery->getParameters();
foreach($parameters as $k=>$param){
if(!array_key_exists($param->getName(), $parameterMappings)){
$parameters->remove($k);
}
}
$countQuery->setParameters($parameters);
return $countQuery;
}

/**
* {@inheritdoc}
*/
Expand Down
23 changes: 23 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/PaginationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,29 @@ public function testQueryWalkerIsKept()
$this->assertCount(1, $paginator->getIterator());
$this->assertEquals(1, $paginator->count());
}

public function testCountQuery_ParametersInSelect()
{
/** @var $query Query */
$query = $this->_em->createQuery(
'SELECT u, (CASE WHEN u.id < :vipMaxId THEN 1 ELSE 0 END) AS hidden promotedFirst FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id < :id or 1=1 ORDER BY promotedFirst'
);
$query->setParameter('vipMaxId', 10);
$query->setParameter('id', 100);
$query->setFirstResult(null)->setMaxResults(null);

$paginator = new Paginator($query);
$countQuery = $paginator->getCountQuery();
$this->assertEquals(2, count($countQuery->getParameters()));
$this->assertEquals(3, $paginator->count());

$query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Query\SqlWalker');
$paginator = new Paginator($query);
$countQuery = $paginator->getCountQuery();
//if select part of query is replaced with count(...) paginator should remove parameters from query object not used in new query.
$this->assertEquals(1, count($countQuery->getParameters()));
$this->assertEquals(3, $paginator->count());
}

public function populate()
{
Expand Down