From 6a69b4700ca0d30ac6ae6f793e46f53b242c6daf Mon Sep 17 00:00:00 2001 From: Norbert Orzechowicz Date: Thu, 7 Feb 2013 14:44:55 +0100 Subject: [PATCH] [DDC-2282] Fix pagination problem with SQL Server. ORDER BY removed from all count queries when on SQL Server Fixed SQLServer ORDER BY problem in paginator CountOutputWalker Added test to check query with ORDER BY and SQLServerPlatform --- .../ORM/Tools/Pagination/CountOutputWalker.php | 4 ++++ .../Tools/Pagination/CountOutputWalkerTest.php | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/Doctrine/ORM/Tools/Pagination/CountOutputWalker.php b/lib/Doctrine/ORM/Tools/Pagination/CountOutputWalker.php index 401898011..a65c73328 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/CountOutputWalker.php +++ b/lib/Doctrine/ORM/Tools/Pagination/CountOutputWalker.php @@ -79,6 +79,10 @@ class CountOutputWalker extends SqlWalker */ public function walkSelectStatement(SelectStatement $AST) { + if ($this->platform->getName() === "mssql") { + $AST->orderByClause = null; + } + $sql = parent::walkSelectStatement($AST); // Find out the SQL alias of the identifier column of the root entity diff --git a/tests/Doctrine/Tests/ORM/Tools/Pagination/CountOutputWalkerTest.php b/tests/Doctrine/Tests/ORM/Tools/Pagination/CountOutputWalkerTest.php index 688f101b0..f3c2e1b50 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Pagination/CountOutputWalkerTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Pagination/CountOutputWalkerTest.php @@ -41,5 +41,22 @@ class CountOutputWalkerTest extends PaginationTestCase "SELECT COUNT(*) AS dctrn_count FROM (SELECT DISTINCT id1 FROM (SELECT count(u0_.id) AS sclr0, g1_.id AS id1, u0_.id AS id2 FROM groups g1_ LEFT JOIN user_group u2_ ON g1_.id = u2_.group_id LEFT JOIN User u0_ ON u0_.id = u2_.user_id GROUP BY g1_.id HAVING sclr0 > 0) dctrn_result) dctrn_table", $query->getSql() ); } + + public function testCountQueryOrderBySqlServer() + { + if ($this->entityManager->getConnection()->getDatabasePlatform()->getName() !== "mssql") { + $this->markTestSkipped('SQLServer only test.'); + } + + $query = $this->entityManager->createQuery( + 'SELECT p FROM Doctrine\Tests\ORM\Tools\Pagination\BlogPost p ORDER BY p.id'); + $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\CountOutputWalker'); + $query->setFirstResult(null)->setMaxResults(null); + + $this->assertEquals( + "SELECT COUNT(*) AS dctrn_count FROM (SELECT DISTINCT id0 FROM (SELECT b0_.id AS id0, b0_.author_id AS author_id1, b0_.category_id AS category_id2 FROM BlogPost b0_) dctrn_result) dctrn_table", + $query->getSql() + ); + } }