-
Notifications
You must be signed in to change notification settings - Fork 138
Prioritize sorted orderBy item when already present in QueryBuilder #350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -71,21 +71,20 @@ public function walkSelectStatement(SelectStatement $AST): void | |||||
| $orderByItem->type = $direction; | ||||||
|
|
||||||
| if ($AST->orderByClause) { | ||||||
| $set = false; | ||||||
| foreach ($AST->orderByClause->orderByItems as $item) { | ||||||
| $orderByItems = &$AST->orderByClause->orderByItems; | ||||||
| foreach ($orderByItems as $orderByIndex => $item) { | ||||||
| if ( | ||||||
| $item->expression instanceof PathExpression && | ||||||
| $item->expression->identificationVariable === $alias && | ||||||
| $item->expression->field === $field | ||||||
| ) { | ||||||
| $item->type = $direction; | ||||||
| $set = true; | ||||||
| unset($orderByItems[$orderByIndex]); | ||||||
|
|
||||||
| break; | ||||||
|
Comment on lines
+82
to
83
|
||||||
| break; |
Copilot
AI
Mar 25, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change alters behavior when the sorted field is already present in an existing ORDER BY (it now removes the existing item and prepends the sorted one). There doesn’t appear to be test coverage for the “existing ORDER BY contains the same field” scenario in the Doctrine ORM sortable tests; please add/adjust a test to assert (1) the sorted field becomes the first ORDER BY item and (2) no duplicate ORDER BY entry remains.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The duplicate-removal logic only matches existing ORDER BY items where the expression is a PathExpression (alias.field). Sorts that use a result/scalar alias (e.g., the existing test case that sorts by
counter) or other expression types won’t be de-duplicated, so you can still end up with duplicate ORDER BY entries even though the PR description says duplicates are removed when already present. If de-duplication is intended broadly, extend the match to handle non-PathExpression expressions when$alias === false(and/or other AST expression node types).