From 5c4b6a214084107a99fbedb76b056a89871e92d1 Mon Sep 17 00:00:00 2001 From: Bill Schaller Date: Tue, 17 Mar 2015 18:44:03 -0400 Subject: [PATCH] resolve nitpicks from @Ocramius and @deeky666 --- .../Pagination/LimitSubqueryOutputWalker.php | 40 ++++++++++--------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php index 0616562c2..2366b5b63 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php +++ b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php @@ -291,24 +291,26 @@ class LimitSubqueryOutputWalker extends SqlWalker { // If the sql statement has an order by clause, we need to wrap it in a new select distinct // statement - if ($orderByClause instanceof OrderByClause) { - // Rebuild the order by clause to work in the scope of the new select statement - /** @var array $sqlOrderColumns an array of items that need to be included in the select list */ - /** @var array $orderBy an array of rebuilt order by items */ - list($sqlOrderColumns, $orderBy) = $this->rebuildOrderByClauseForOuterScope($orderByClause); - - // Identifiers are always included in the select list, so there's no need to include them twice - $sqlOrderColumns = array_diff($sqlOrderColumns, $sqlIdentifier); - - // Build the select distinct statement - $sql = sprintf( - 'SELECT DISTINCT %s FROM (%s) dctrn_result ORDER BY %s', - implode(', ', array_merge($sqlIdentifier, $sqlOrderColumns)), - $innerSql, - implode(', ', $orderBy) - ); + if (!$orderByClause instanceof OrderByClause) { + return $sql; } + // Rebuild the order by clause to work in the scope of the new select statement + /* @var array $sqlOrderColumns an array of items that need to be included in the select list */ + /* @var array $orderBy an array of rebuilt order by items */ + list($sqlOrderColumns, $orderBy) = $this->rebuildOrderByClauseForOuterScope($orderByClause); + + // Identifiers are always included in the select list, so there's no need to include them twice + $sqlOrderColumns = array_diff($sqlOrderColumns, $sqlIdentifier); + + // Build the select distinct statement + $sql = sprintf( + 'SELECT DISTINCT %s FROM (%s) dctrn_result ORDER BY %s', + implode(', ', array_merge($sqlIdentifier, $sqlOrderColumns)), + $innerSql, + implode(', ', $orderBy) + ); + return $sql; } @@ -318,14 +320,14 @@ class LimitSubqueryOutputWalker extends SqlWalker * @param OrderByClause $orderByClause * @return array */ - protected function rebuildOrderByClauseForOuterScope(OrderByClause $orderByClause) { + private function rebuildOrderByClauseForOuterScope(OrderByClause $orderByClause) { $dqlAliasToSqlTableAliasMap = $searchPatterns = $replacements = $dqlAliasToClassMap = $selectListAdditions = $orderByItems - = array(); + = []; // Generate DQL alias -> SQL table alias mapping foreach(array_keys($this->rsm->aliasMap) as $dqlAlias) { @@ -334,7 +336,7 @@ class LimitSubqueryOutputWalker extends SqlWalker } // Pattern to find table path expressions in the order by clause - $fieldSearchPattern = "/(?rsm->fieldMappings as $fieldAlias => $columnName) {