Skip to content
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

orderBy not being respected #11163

Closed
brian-codes opened this issue Jan 17, 2024 · 8 comments · Fixed by #11422
Closed

orderBy not being respected #11163

brian-codes opened this issue Jan 17, 2024 · 8 comments · Fixed by #11422

Comments

@brian-codes
Copy link

brian-codes commented Jan 17, 2024

Bug Report

Q A
BC Break yes/no
Version 2.17.2

Summary

I have a relationship between Threads and Messages. One thread can contain many Messages. Using XML mapping there is an orderBy entry stating the collection of messages should be ordered by their created at date. This works correctly on v2.16.3 but open upgrading to 2.17.2 this is no longer respected and the collection of messages is ordered by it's UUID.

Current behavior

orderBy entry not being respected and instead sorting the collection by UUID

How to reproduce

Use QueryBuilder to get a collection of Threads.

    <entity repository-class="App\ThreadRepository" name="App\Thread" table="threads">
        <one-to-many field="messages" target-entity="App\Message" mapped-by="thread" fetch="EAGER">
            <order-by>
                <order-by-field name="createdAt"/>
            </order-by>
        </one-to-many>
    </entity>
    <entity repository-class="App\MessageRepository" name="App\Message" table="messages">
        <field name="createdAt" type="datetime" column="created_at"/>
        <many-to-one field="thread" target-entity="App\Thread" inversed-by="messages"/>
    </entity>
    $qb = $this->getEntityManager()->createQueryBuilder();

    $qb
    ->select('thread')
    ->from(Thread::class, 'thread');

    return $qb->getQuery()->getResult();

Expected behavior

orderBy entry to be respected and order the collection by the stated field.

@mpdude
Copy link
Contributor

mpdude commented Jan 20, 2024

Could you please check whether it is specific for the XML mapping and/or using fetch=EAGER?

@grossmannmartin
Copy link

the same problem occurs with annotation mapping (learned the hard way today 😆 )

@greg0ire
Copy link
Member

@grossmannmartin are you using fetch=EAGER?

@grossmannmartin
Copy link

Yes, Yes I was.
In the end, I rewrote it without EAGER and that resolved my issue.

@greg0ire
Copy link
Member

Then it is probably caused by #8391, Cc @beberlei

@Alvaro948
Copy link

Alvaro948 commented Feb 21, 2024

Tested con 2.18.0, does not work:

#[ORM\OneToMany(mappedBy: 'parent', targetEntity: VariableEntity::class, fetch: 'EAGER')]
#[ORM\OrderBy(value: ['name' => 'ASC'])]
private Collection $variables;

@cttboehme
Copy link

We have hit the same problem of the orderBy attribute not being respected.
And yes, the suspected #8391 is the cuprit.
In particular, this line is at fault:

https://github.com/beberlei/doctrine2/blob/ec74c83845231fa913b387a9cc1589d5586d0e0e/lib/Doctrine/ORM/UnitOfWork.php#L3182C1-L3183C1

If I change this to:

$found = $this->getEntityPersister($targetEntity)->loadAll([$mappedBy => $entities], $mapping['orderBy']);

all is well again (at least for our simple case).

tomasz-ryba pushed a commit to tomasz-ryba/orm that referenced this issue Apr 23, 2024
Fetch EAGER mode ignores orderBy as of changes introduced with doctrine#8391
Fixes duplicated doctrine#11381
tomasz-ryba pushed a commit to tomasz-ryba/orm that referenced this issue Apr 23, 2024
Fetch EAGER mode ignores orderBy as of changes introduced with doctrine#8391
Fixes duplicated doctrine#11381
tomasz-ryba pushed a commit to tomasz-ryba/orm that referenced this issue Apr 24, 2024
Fetch EAGER mode ignores orderBy as of changes introduced with doctrine#8391
Fixes duplicated doctrine#11381
tomasz-ryba pushed a commit to tomasz-ryba/orm that referenced this issue Apr 24, 2024
Fetch EAGER mode ignores orderBy as of changes introduced with doctrine#8391
Fixes duplicated doctrine#11381
@greg0ire greg0ire linked a pull request Apr 24, 2024 that will close this issue
@brian-codes
Copy link
Author

Thankyou @tomasz-ryba @greg0ire

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants