DDC-3448: @OrderBy on eager @OneToMany does not work #4253

Open
opened 2026-01-22 14:38:12 +01:00 by admin · 11 comments
Owner

Originally created by @doctrinebot on GitHub (Dec 13, 2014).

Jira issue originally created by user backbone:

generated code when eagerly fetching:

SELECT 
  t0.id AS id_8,
  t19.category*id AS category_id*23 
FROM 
  category t0 
  LEFT JOIN attribute*category t19 ON t19.category*id = t0.id 
WHERE 
  t0.id = ?

when fetching lazy the collection query has the ORDER BY clause

Originally created by @doctrinebot on GitHub (Dec 13, 2014). Jira issue originally created by user backbone: generated code when eagerly fetching: ``` sql SELECT t0.id AS id_8, t19.category*id AS category_id*23 FROM category t0 LEFT JOIN attribute*category t19 ON t19.category*id = t0.id WHERE t0.id = ? ``` when fetching lazy the collection query has the ORDER BY clause
admin added the Bug label 2026-01-22 14:38:12 +01:00
Author
Owner

@guilhermeblanco commented on GitHub (May 19, 2016):

Copied from #3885: It's not an easy solution, because ordering needs only to apply for the association, not for the top-level entities though. This would require the join to not happen as part of main query, but immediately after.

@guilhermeblanco commented on GitHub (May 19, 2016): Copied from #3885: It's not an easy solution, because ordering needs only to apply for the association, not for the top-level entities though. This would require the join to not happen as part of main query, but immediately after.
Author
Owner

@aistis- commented on GitHub (Oct 20, 2016):

Run into the same problem. Does anyone has any workaround for this?

EDIT: found a work around http://stackoverflow.com/questions/16705425/usort-a-doctrine-common-collections-arraycollection#answer-24246304

@aistis- commented on GitHub (Oct 20, 2016): Run into the same problem. Does anyone has any workaround for this? EDIT: found a work around http://stackoverflow.com/questions/16705425/usort-a-doctrine-common-collections-arraycollection#answer-24246304
Author
Owner

@Ocramius commented on GitHub (Oct 25, 2016):

Current workaround is multi-step hydration - https://ocramius.github.io/blog/doctrine-orm-optimization-hydration/

Not fun, not simple, but works and is fairly efficient.

@Ocramius commented on GitHub (Oct 25, 2016): Current workaround is multi-step hydration - https://ocramius.github.io/blog/doctrine-orm-optimization-hydration/ Not fun, not simple, but works and is fairly efficient.
Author
Owner

@quisse commented on GitHub (Feb 18, 2019):

Any update on this?

@quisse commented on GitHub (Feb 18, 2019): Any update on this?
Author
Owner

@beberlei commented on GitHub (Feb 16, 2020):

This might be fixed by https://github.com/doctrine/orm/pull/7850 but never got a reference.

@beberlei commented on GitHub (Feb 16, 2020): This might be fixed by https://github.com/doctrine/orm/pull/7850 but never got a reference.
Author
Owner

@secit-pl commented on GitHub (Jul 24, 2023):

The problem still exists:

Here all elements are sorted by position:

    /**
     * @ORM\OneToMany(targetEntity=File::class, mappedBy="material", cascade={"all"}, orphanRemoval=true, fetch="LAZY")
     * @ORM\OrderBy({"position" = "ASC"})
     */
    private Collection $files;

here "position" is ignored and all fields are sorted by primary key:

    /**
     * @ORM\OneToMany(targetEntity=File::class, mappedBy="material", cascade={"all"}, orphanRemoval=true, fetch="EAGER")
     * @ORM\OrderBy({"position" = "ASC"})
     */
    private Collection $files;

Used packages:
doctrine/orm - 2.9.6
doctrine/doctrine-bundle - 2.5.7
doctrine/annotations - 1.14.3

@secit-pl commented on GitHub (Jul 24, 2023): The problem still exists: Here all elements are sorted by position: ```php /** * @ORM\OneToMany(targetEntity=File::class, mappedBy="material", cascade={"all"}, orphanRemoval=true, fetch="LAZY") * @ORM\OrderBy({"position" = "ASC"}) */ private Collection $files; ``` here "position" is ignored and all fields are sorted by primary key: ```php /** * @ORM\OneToMany(targetEntity=File::class, mappedBy="material", cascade={"all"}, orphanRemoval=true, fetch="EAGER") * @ORM\OrderBy({"position" = "ASC"}) */ private Collection $files; ``` Used packages: doctrine/orm - 2.9.6 doctrine/doctrine-bundle - 2.5.7 doctrine/annotations - 1.14.3
Author
Owner

@derrabus commented on GitHub (Jul 24, 2023):

The problem still exists:
[…]
Used packages: doctrine/orm - 2.9.6

That release is almost two years old. I guess that does not really count as "still exists". 😉

@derrabus commented on GitHub (Jul 24, 2023): > The problem still exists: > […] > Used packages: doctrine/orm - 2.9.6 That release is almost two years old. I guess that does not really count as "still exists". 😉
Author
Owner

@krugerman007 commented on GitHub (Aug 20, 2023):

What about updates?

doctrine/orm - 2.16.1 - problem exists :(

@krugerman007 commented on GitHub (Aug 20, 2023): What about updates? doctrine/orm - 2.16.1 - problem exists :(
Author
Owner

@schwierBD commented on GitHub (Nov 9, 2023):

Any Update?

@schwierBD commented on GitHub (Nov 9, 2023): Any Update?
Author
Owner

@oleg-andreyev commented on GitHub (Mar 5, 2024):

Fix looks like quite easy:
\Doctrine\ORM\UnitOfWork::eagerLoadCollections
when calling loadAll, just pass orderBy mapping.

¯_(ツ)_/¯

@oleg-andreyev commented on GitHub (Mar 5, 2024): Fix looks like quite easy: \Doctrine\ORM\UnitOfWork::eagerLoadCollections when calling loadAll, just pass `orderBy` mapping. ¯\_(ツ)_/¯
Author
Owner

@krugerman007 commented on GitHub (Feb 26, 2025):

We are still waiting for a fix :/(

@krugerman007 commented on GitHub (Feb 26, 2025): We are still waiting for a fix :/(
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#4253