Skip to content

Commit f45cf26

Browse files
committed
Merge branch 'hotfix/#1211-DDC-3434-backport-to-2.4' into 2.4
Close #1211
2 parents 801e7f0 + 4d846c1 commit f45cf26

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,26 +93,23 @@ public function __construct($query, $parserResult, array $queryComponents)
9393
*/
9494
public function walkSelectStatement(SelectStatement $AST)
9595
{
96-
if ($this->platform instanceof PostgreSqlPlatform) {
97-
// Set every select expression as visible(hidden = false) to
98-
// make $AST to have scalar mappings properly
99-
$hiddens = array();
100-
foreach ($AST->selectClause->selectExpressions as $idx => $expr) {
101-
$hiddens[$idx] = $expr->hiddenAliasResultVariable;
102-
$expr->hiddenAliasResultVariable = false;
103-
}
96+
// Set every select expression as visible(hidden = false) to
97+
// make $AST have scalar mappings properly - this is relevant for referencing selected
98+
// fields from outside the subquery, for example in the ORDER BY segment
99+
$hiddens = array();
100+
101+
foreach ($AST->selectClause->selectExpressions as $idx => $expr) {
102+
$hiddens[$idx] = $expr->hiddenAliasResultVariable;
103+
$expr->hiddenAliasResultVariable = false;
104+
}
104105

105-
$innerSql = parent::walkSelectStatement($AST);
106+
$innerSql = parent::walkSelectStatement($AST);
106107

107-
// Restore hiddens
108-
foreach ($AST->selectClause->selectExpressions as $idx => $expr) {
109-
$expr->hiddenAliasResultVariable = $hiddens[$idx];
110-
}
111-
} else {
112-
$innerSql = parent::walkSelectStatement($AST);
108+
// Restore hiddens
109+
foreach ($AST->selectClause->selectExpressions as $idx => $expr) {
110+
$expr->hiddenAliasResultVariable = $hiddens[$idx];
113111
}
114112

115-
116113
// Find out the SQL alias of the identifier column of the root entity.
117114
// It may be possible to make this work with multiple root entities but that
118115
// would probably require issuing multiple queries or doing a UNION SELECT.

tests/Doctrine/Tests/ORM/Tools/Pagination/LimitSubqueryOutputWalkerTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public function testCountQueryMixedResultsWithName()
200200
public function testCountQueryWithArithmeticOrderByCondition()
201201
{
202202
$query = $this->entityManager->createQuery(
203-
'SELECT a FROM Doctrine\\Tests\\ORM\\Tools\\Pagination\\Author a ORDER BY (1 - 1000) * 1 DESC'
203+
'SELECT a FROM Doctrine\Tests\ORM\Tools\Pagination\Author a ORDER BY (1 - 1000) * 1 DESC'
204204
);
205205
$this->entityManager->getConnection()->setDatabasePlatform(new MySqlPlatform());
206206

@@ -211,5 +211,22 @@ public function testCountQueryWithArithmeticOrderByCondition()
211211
$query->getSQL()
212212
);
213213
}
214+
215+
/**
216+
* @group DDC-3434
217+
*/
218+
public function testLimitSubqueryWithHiddenSelectionInOrderBy()
219+
{
220+
$query = $this->entityManager->createQuery(
221+
'SELECT a, a.name AS HIDDEN ord FROM Doctrine\Tests\ORM\Tools\Pagination\Author a ORDER BY ord DESC'
222+
);
223+
224+
$query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker');
225+
226+
$this->assertEquals(
227+
'SELECT DISTINCT id0, name2 FROM (SELECT a0_.id AS id0, a0_.name AS name1, a0_.name AS name2 FROM Author a0_ ORDER BY name2 DESC) dctrn_result ORDER BY name2 DESC',
228+
$query->getSql()
229+
);
230+
}
214231
}
215232

0 commit comments

Comments
 (0)