DDC-2260: Partial DQL query doesn't respect given order of columns #2842

Open
opened 2026-01-22 14:05:31 +01:00 by admin · 3 comments
Owner

Originally created by @doctrinebot on GitHub (Jan 26, 2013).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user flying:

When executing partial DQL queries it may be important to keep given order of columns e.g. for "pairs" hydrator when first column of a pair is used as a key and second - as value. For example query like this:
sqlselect partial u.{id,name} from my:User u
will expect "id" to be first in resulted set and "name" to be second and not vice versa.

However Doctrine parses this part of statement via iterating over fields mapping from entity's class metadata (as can be seen in Doctrine\ORM\Query\SqlWalker::walkSelectExpression()):

  if ($partialFieldSet && ! in_array($fieldName, $partialFieldSet)) {
    continue;
  }
  ...

and hence given columns order preserving is not guaranteed.

Originally created by @doctrinebot on GitHub (Jan 26, 2013). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user flying: When executing partial DQL queries it may be important to keep given order of columns e.g. for "pairs" hydrator when first column of a pair is used as a key and second - as value. For example query like this: `sqlselect partial u.{id,name} from my:User u` will expect "id" to be first in resulted set and "name" to be second and not vice versa. However Doctrine parses this part of statement via iterating over fields mapping from entity's class metadata (as can be seen in `Doctrine\ORM\Query\SqlWalker::walkSelectExpression()`): ```foreach ($class->fieldMappings as $fieldName => $mapping) { if ($partialFieldSet && ! in_array($fieldName, $partialFieldSet)) { continue; } ... ``` and hence given columns order preserving is not guaranteed. ```
admin added the Improvement label 2026-01-22 14:05:31 +01:00
Author
Owner

@doctrinebot commented on GitHub (Jan 26, 2013):

Comment created by @ocramius:

What is the advantage in respecting the order given in the DQL query?

@doctrinebot commented on GitHub (Jan 26, 2013): Comment created by @ocramius: What is the advantage in respecting the order given in the DQL query?
Author
Owner

@doctrinebot commented on GitHub (Jan 27, 2013):

Comment created by flying:

Currently the only practical reason for it that I found is "pairs" hydrator. However it is, of course, possible to implement it without such change too.

Generally speaking this behavior (getting result set with same order of columns that was given in a query) is something that is feeling "natural" for operations with database since it is how you normally get results from SQL queries.

Maybe it will be enough to mention in documentation for Doctrine that given columns order is not guaranteed to be kept.

@doctrinebot commented on GitHub (Jan 27, 2013): Comment created by flying: Currently the only practical reason for it that I found is "pairs" hydrator. However it is, of course, possible to implement it without such change too. Generally speaking this behavior (getting result set with same order of columns that was given in a query) is something that is feeling "natural" for operations with database since it is how you normally get results from SQL queries. Maybe it will be enough to mention in documentation for Doctrine that given columns order is not guaranteed to be kept.
Author
Owner

@doctrinebot commented on GitHub (Jan 27, 2013):

Comment created by @ocramius:

@[~flying] I don't think it's worth mentioning it. Also, including a fix for this is quite complex. If you prefer to document it, go for it!

@doctrinebot commented on GitHub (Jan 27, 2013): Comment created by @ocramius: @[~flying] I don't think it's worth mentioning it. Also, including a fix for this is quite complex. If you prefer to document it, go for it!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#2842