diff --git a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php index 45a6ce78be7..a37ab5645af 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php +++ b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php @@ -13,6 +13,7 @@ namespace Doctrine\ORM\Tools\Pagination; +use Doctrine\ORM\Query\AST\PathExpression; use Doctrine\ORM\Query\SqlWalker; use Doctrine\ORM\Query\AST\SelectStatement; use Doctrine\DBAL\Platforms\PostgreSqlPlatform; @@ -200,12 +201,14 @@ public function preserveSqlOrdering(SelectStatement $AST, array $sqlIdentifier, $orderBy = array(); if (isset($AST->orderByClause)) { foreach ($AST->orderByClause->orderByItems as $item) { - $possibleAliases = (is_object($item->expression)) - ? array_keys($this->rsm->fieldMappings, $item->expression->field) - : array_keys($this->rsm->scalarMappings, $item->expression); + $expression = $item->expression; + + $possibleAliases = $expression instanceof PathExpression + ? array_keys($this->rsm->fieldMappings, $expression->field) + : array_keys($this->rsm->scalarMappings, $expression); foreach ($possibleAliases as $alias) { - if (!is_object($item->expression) || $this->rsm->columnOwnerMap[$alias] == $item->expression->identificationVariable) { + if (!is_object($expression) || $this->rsm->columnOwnerMap[$alias] == $expression->identificationVariable) { $sqlOrderColumns[] = $alias; $orderBy[] = $alias . ' ' . $item->type; break; diff --git a/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php b/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php index 7737b35a410..e7c5ae2b640 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php @@ -2,6 +2,9 @@ namespace Doctrine\Tests\ORM\Tools\Pagination; +use Doctrine\DBAL\Platforms\MySqlPlatform; +use Doctrine\DBAL\Platforms\OraclePlatform; +use Doctrine\DBAL\Platforms\PostgreSqlPlatform; use Doctrine\ORM\Query; class LimitSubqueryOutputWalkerTest extends PaginationTestCase @@ -22,7 +25,7 @@ public function testLimitSubquery() public function testLimitSubqueryWithSortPg() { $odp = $this->entityManager->getConnection()->getDatabasePlatform(); - $this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform); + $this->entityManager->getConnection()->setDatabasePlatform(new PostgreSqlPlatform); $query = $this->entityManager->createQuery( 'SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category c JOIN p.author a ORDER BY p.title'); @@ -39,7 +42,7 @@ public function testLimitSubqueryWithSortPg() public function testLimitSubqueryWithScalarSortPg() { $odp = $this->entityManager->getConnection()->getDatabasePlatform(); - $this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform); + $this->entityManager->getConnection()->setDatabasePlatform(new PostgreSqlPlatform); $query = $this->entityManager->createQuery( 'SELECT u, g, COUNT(g.id) AS g_quantity FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.groups g ORDER BY g_quantity' @@ -58,7 +61,7 @@ public function testLimitSubqueryWithScalarSortPg() public function testLimitSubqueryWithMixedSortPg() { $odp = $this->entityManager->getConnection()->getDatabasePlatform(); - $this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform); + $this->entityManager->getConnection()->setDatabasePlatform(new PostgreSqlPlatform); $query = $this->entityManager->createQuery( 'SELECT u, g, COUNT(g.id) AS g_quantity FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.groups g ORDER BY g_quantity, u.id DESC' @@ -77,7 +80,7 @@ public function testLimitSubqueryWithMixedSortPg() public function testLimitSubqueryWithHiddenScalarSortPg() { $odp = $this->entityManager->getConnection()->getDatabasePlatform(); - $this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform); + $this->entityManager->getConnection()->setDatabasePlatform(new PostgreSqlPlatform); $query = $this->entityManager->createQuery( 'SELECT u, g, COUNT(g.id) AS hidden g_quantity FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.groups g ORDER BY g_quantity, u.id DESC' @@ -96,7 +99,7 @@ public function testLimitSubqueryWithHiddenScalarSortPg() public function testLimitSubqueryPg() { $odp = $this->entityManager->getConnection()->getDatabasePlatform(); - $this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform); + $this->entityManager->getConnection()->setDatabasePlatform(new PostgreSqlPlatform); $this->testLimitSubquery(); @@ -106,7 +109,7 @@ public function testLimitSubqueryPg() public function testLimitSubqueryWithSortOracle() { $odp = $this->entityManager->getConnection()->getDatabasePlatform(); - $this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\OraclePlatform); + $this->entityManager->getConnection()->setDatabasePlatform(new OraclePlatform); $query = $this->entityManager->createQuery( 'SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category c JOIN p.author a ORDER BY p.title'); @@ -124,7 +127,7 @@ public function testLimitSubqueryWithSortOracle() public function testLimitSubqueryWithScalarSortOracle() { $odp = $this->entityManager->getConnection()->getDatabasePlatform(); - $this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\OraclePlatform); + $this->entityManager->getConnection()->setDatabasePlatform(new OraclePlatform); $query = $this->entityManager->createQuery( 'SELECT u, g, COUNT(g.id) AS g_quantity FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.groups g ORDER BY g_quantity' @@ -144,7 +147,7 @@ public function testLimitSubqueryWithScalarSortOracle() public function testLimitSubqueryWithMixedSortOracle() { $odp = $this->entityManager->getConnection()->getDatabasePlatform(); - $this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\OraclePlatform); + $this->entityManager->getConnection()->setDatabasePlatform(new OraclePlatform); $query = $this->entityManager->createQuery( 'SELECT u, g, COUNT(g.id) AS g_quantity FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.groups g ORDER BY g_quantity, u.id DESC' @@ -164,7 +167,7 @@ public function testLimitSubqueryWithMixedSortOracle() public function testLimitSubqueryOracle() { $odp = $this->entityManager->getConnection()->getDatabasePlatform(); - $this->entityManager->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\OraclePlatform); + $this->entityManager->getConnection()->setDatabasePlatform(new OraclePlatform); $query = $this->entityManager->createQuery( 'SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category c JOIN p.author a'); @@ -179,7 +182,7 @@ public function testLimitSubqueryOracle() $this->entityManager->getConnection()->setDatabasePlatform($odp); } - public function testCountQuery_MixedResultsWithName() + public function testCountQueryMixedResultsWithName() { $query = $this->entityManager->createQuery( 'SELECT a, sum(a.name) as foo FROM Doctrine\Tests\ORM\Tools\Pagination\Author a'); @@ -190,5 +193,23 @@ public function testCountQuery_MixedResultsWithName() "SELECT DISTINCT id_0 FROM (SELECT a0_.id AS id_0, a0_.name AS name_1, sum(a0_.name) AS sclr_2 FROM Author a0_) dctrn_result", $limitQuery->getSql() ); } + + /** + * @group DDC-3336 + */ + public function testCountQueryWithArithmeticOrderByCondition() + { + $query = $this->entityManager->createQuery( + 'SELECT a FROM Doctrine\\Tests\\ORM\\Tools\\Pagination\\Author a ORDER BY (1 - 1000) * 1 DESC' + ); + $this->entityManager->getConnection()->setDatabasePlatform(new MySqlPlatform()); + + $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker'); + + $this->assertSame( + 'SELECT DISTINCT id_0 FROM (SELECT a0_.id AS id_0, a0_.name AS name_1 FROM Author a0_ ORDER BY (1 - 1000) * 1 DESC) dctrn_result', + $query->getSQL() + ); + } } diff --git a/tests/Doctrine/Tests/ORM/Tools/Pagination/PaginationTestCase.php b/tests/Doctrine/Tests/ORM/Tools/Pagination/PaginationTestCase.php index d503e81aac4..d3e77f6f06c 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Pagination/PaginationTestCase.php +++ b/tests/Doctrine/Tests/ORM/Tools/Pagination/PaginationTestCase.php @@ -6,6 +6,9 @@ abstract class PaginationTestCase extends OrmTestCase { + /** + * @var \Doctrine\ORM\EntityManagerInterface + */ public $entityManager; public function setUp()