Skip to content

Commit

Permalink
Paginate over queries that ORDER BY a mixed result
Browse files Browse the repository at this point in the history
This change makes the Paginate extension work on queries that use ORDER
BY to order the results in a mixed resultset, such as for example:

SELECT g, COUNT(u.id) AS numUsers FROM GROUP g LEFT JOIN g.users u GROUP BY g.id ORDER BY numUsers DESC
  • Loading branch information
sandermarechal committed Oct 11, 2011
1 parent 8999bea commit cc0e950
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions lib/DoctrineExtensions/Paginate/LimitSubqueryWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,19 @@ public function walkSelectStatement(SelectStatement $AST)
{
$parent = null;
$parentName = null;
foreach ($this->_getQueryComponents() AS $dqlAlias => $qComp) {
$selectExpressions = array();

// skip mixed data in query
foreach ($this->_getQueryComponents() AS $dqlAlias => $qComp) {
// preserve mixed data in query for ordering
if (isset($qComp['resultVariable'])) {
$selectExpressions[] = new SelectExpression($qComp['resultVariable'], $dqlAlias);
continue;
}

if ($qComp['parent'] === null && $qComp['nestingLevel'] == 0) {
$parent = $qComp;
$parentName = $dqlAlias;
break;
continue;
}
}

Expand All @@ -71,19 +73,20 @@ public function walkSelectStatement(SelectStatement $AST)
);
$pathExpression->type = PathExpression::TYPE_STATE_FIELD;

$AST->selectClause->selectExpressions = array(
new SelectExpression($pathExpression, '_dctrn_id')
);
array_unshift($selectExpressions, new SelectExpression($pathExpression, '_dctrn_id'));
$AST->selectClause->selectExpressions = $selectExpressions;

if (isset($AST->orderByClause)) {
foreach ($AST->orderByClause->orderByItems as $item) {
$pathExpression = new PathExpression(
PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION,
$item->expression->identificationVariable,
$item->expression->field
);
$pathExpression->type = PathExpression::TYPE_STATE_FIELD;
$AST->selectClause->selectExpressions[] = new SelectExpression($pathExpression, '_dctrn_ord' . $this->_aliasCounter++);
if ($item->expression instanceof PathExpression) {
$pathExpression = new PathExpression(
PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION,
$item->expression->identificationVariable,
$item->expression->field
);
$pathExpression->type = PathExpression::TYPE_STATE_FIELD;
$AST->selectClause->selectExpressions[] = new SelectExpression($pathExpression, '_dctrn_ord' . $this->_aliasCounter++);
}
}
}

Expand Down

0 comments on commit cc0e950

Please sign in to comment.