From d2501a9e4a43126c8ac65ce91d56849d1d205610 Mon Sep 17 00:00:00 2001 From: Sander Marechal Date: Wed, 7 Mar 2012 08:42:09 +0100 Subject: [PATCH] Throw exception when using the CountWalker with a HAVING query --- .../ORM/Tools/Pagination/CountWalker.php | 4 ++++ .../Tests/ORM/Functional/PaginationTest.php | 12 ++++++------ .../ORM/Tools/Pagination/CountWalkerTest.php | 16 ++++++++++++++++ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/Pagination/CountWalker.php b/lib/Doctrine/ORM/Tools/Pagination/CountWalker.php index 10df1c3e1..3070437d3 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/CountWalker.php +++ b/lib/Doctrine/ORM/Tools/Pagination/CountWalker.php @@ -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) diff --git a/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php b/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php index cc5354dce..119f73b7e 100644 --- a/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php @@ -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 { - count($paginator); - $this->fail('Paginator did not detect custom SQL walker'); - } catch (\PHPUnit_Framework_Error_Notice $e) { - $this->assertEquals('Undefined index: userCount', $e->getMessage()); - } + $this->setExpectedException( + 'RuntimeException', + 'Cannot count query that uses a HAVING clause. Use the SQL walkers for pagination' + ); + + count($paginator); } public function populate() diff --git a/tests/Doctrine/Tests/ORM/Tools/Pagination/CountWalkerTest.php b/tests/Doctrine/Tests/ORM/Tools/Pagination/CountWalkerTest.php index 816339df0..907f6577d 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Pagination/CountWalkerTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Pagination/CountWalkerTest.php @@ -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(); + } }