doctrine / orm

Doctrine Object Relational Mapper (ORM)
https://www.doctrine-project.org/projects/orm.html
MIT License
9.92k stars 2.51k forks source link

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

Open doctrinebot opened 9 years ago

doctrinebot commented 9 years ago

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

guilhermeblanco commented 8 years ago

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.

aistis- commented 7 years ago

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

Ocramius commented 7 years ago

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.

quisse commented 5 years ago

Any update on this?

beberlei commented 4 years ago

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

secit-pl commented 1 year ago

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

derrabus commented 1 year ago

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". 😉

krugerman007 commented 1 year ago

What about updates?

doctrine/orm - 2.16.1 - problem exists :(

schwierBD commented 10 months ago

Any Update?

oleg-andreyev commented 6 months ago

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

¯_(ツ)_/¯