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

PersistentCollection::matching() merges orderings incorrectly when association has default sorting #7836

Closed
paxuclus opened this issue Sep 27, 2019 · 3 comments
Assignees
Labels
Milestone

Comments

@paxuclus
Copy link
Contributor

paxuclus commented Sep 27, 2019

Bug Report

When PersistentCollection::matching() is called with orderings, they are not correctly merged together with the default sortings of an association.

Q A
BC Break yes/no
Version 2.6.4

Summary

When a relationship has a default sorting configured using @ORM\OrderBy({"lastName" = "ASC", "firstName" = "ASC"}), the orderings are not correctly merged together with the orderings of the $criteria in PersistentCollection::matching().

Current behavior

The orderings will be sorted like the array given to the @ORM\OrderBy annotation, leading to unexpected results when passing another orderBy to the $criteria of PersistentCollection::matching().

How to reproduce

  • Define a relationship with @ORM\OrderBy({"lastName" = "ASC", "firstName" = "ASC"}) (sort by at least 2 properties)
  • Call $relationship->matching(Criteria::create()->orderBy(['firstName' => 'DESC'])) on the PersistentCollection of that relationship

Expected behavior

The collection should be ordered by firstName DESC, lastName ASC, but instead it will be ordered by lastName ASC, firstName DESC.

Origin of the bug

Original Issue: #7767
The bug was introduced in this PR: https://github.com/doctrine/orm/pull/7766/files#diff-108586f774fc8acb02163ed709e05e86R675

Using $criteria->orderBy(array_merge($criteria->getOrderings(), $this->association['orderBy'] ?? [], $criteria->getOrderings())); there would fix the ordering.

@SenseException
Copy link
Member

Thank you for reporting this issue. If you like, you can create a PR with a fix and test(s) to this issue.

As far as I understand you describe a regression between two versions/commits for the orderBy behavior. But I'm not sure if there's an intended or defined behavior for a use case like this.

When I read $relationship->matching(Criteria::create()->orderBy(['firstName' => 'DESC'])) shouldn't it override the OrderBy annotation instead of mixing it? I would expect that firstName DESC will be the only order in here.

If you want to create a PR, you should aim to restore the previous known behavior you described, but I wonder if we should change it for the next major release.

@paxuclus
Copy link
Contributor Author

paxuclus commented Oct 1, 2019

Hey @SenseException,

thinking about this again I agree with you that "orderBy()" should override the default sorting.

Previously (before #7766 was merged), the "->matching()" would completely ignore the OrderBy annotation, even if there were no orderings in the $criteria.

I think the PersistentCollection should only use the OrderBy annotation if the $criteria does not contain any orderings.

I will try to create a PR this week. 👍

paxuclus added a commit to paxuclus/orm that referenced this issue Oct 1, 2019
If no orderings are given to PersistentCollection::matching(), the
orderBy annotation will be used if present. If the criteria contains
orderings, those will be used without merging them with the orderBy.

See doctrine#7836
paxuclus added a commit to paxuclus/orm that referenced this issue Oct 1, 2019
If no orderings are given to PersistentCollection::matching(), the
orderBy annotation will be used if present. If the criteria contains
orderings, those will be used without merging them with the orderBy.

See doctrine#7836
@lcobucci lcobucci added the Bug label Oct 1, 2019
@lcobucci lcobucci added this to the 2.6.5 milestone Oct 1, 2019
paxuclus added a commit to paxuclus/orm that referenced this issue Oct 2, 2019
If no orderings are given to PersistentCollection::matching(), the
orderBy annotation will be used if present. If the criteria contains
orderings, those will be used without merging them with the orderBy.

See doctrine#7836
@lcobucci
Copy link
Member

lcobucci commented Oct 2, 2019

Handled by #7850

@lcobucci lcobucci closed this as completed Oct 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants