1
0
mirror of synced 2025-01-18 06:21:40 +03:00

Throw exception when using the CountWalker with a HAVING query

This commit is contained in:
Sander Marechal 2012-03-07 08:42:09 +01:00
parent edd5d14b06
commit d2501a9e4a
3 changed files with 26 additions and 6 deletions

View File

@ -43,6 +43,10 @@ class CountWalker extends TreeWalkerAdapter
*/
public function walkSelectStatement(SelectStatement $AST)
{
if ($AST->havingClause) {
throw new \RuntimeException('Cannot count query that uses a HAVING clause. Use the SQL walkers for pagination');
}
$rootComponents = array();
foreach ($this->_getQueryComponents() AS $dqlAlias => $qComp) {
$isParent = array_key_exists('parent', $qComp)

View File

@ -125,12 +125,12 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase
$query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Query\SqlWalker');
$paginator = new Paginator($query);
try {
$this->setExpectedException(
'RuntimeException',
'Cannot count query that uses a HAVING clause. Use the SQL walkers for pagination'
);
count($paginator);
$this->fail('Paginator did not detect custom SQL walker');
} catch (\PHPUnit_Framework_Error_Notice $e) {
$this->assertEquals('Undefined index: userCount', $e->getMessage());
}
}
public function populate()

View File

@ -74,5 +74,21 @@ class CountWalkerTest extends PaginationTestCase
"SELECT count(DISTINCT b0_.id) AS sclr0 FROM BlogPost b0_ INNER JOIN Category c1_ ON b0_.category_id = c1_.id INNER JOIN Author a2_ ON b0_.author_id = a2_.id", $query->getSql()
);
}
public function testCountQuery_HavingException()
{
$query = $this->entityManager->createQuery(
"SELECT g, COUNT(u.id) AS userCount FROM Doctrine\Tests\Models\CMS\CmsGroup g LEFT JOIN g.users u GROUP BY g.id HAVING userCount > 0"
);
$query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\CountWalker'));
$query->setFirstResult(null)->setMaxResults(null);
$this->setExpectedException(
'RuntimeException',
'Cannot count query that uses a HAVING clause. Use the SQL walkers for pagination'
);
$query->getSql();
}
}