PersistentCollection ignore orderBy on matching($criteria) #5584

Open
opened 2026-01-22 15:11:58 +01:00 by admin · 1 comment
Owner

Originally created by @sven-communitales on GitHub (Jun 22, 2017).

doctrine/orm: 2.5.6

The PersistentCollection::matching() method does not take the ordering of a Criteria in account. For an ArrayCollection is works fine.

Reproduce:

I have two Entities

class Product {

    /**
     * @var FilterAttribute[]|ArrayCollection
     * @ORM\ManyToMany(targetEntity="FilterAttribute")
     * @ORM\JoinTable(name="product_filter_attribute",
     *      joinColumns={@ORM\JoinColumn(name="product_uuid", referencedColumnName="uuid")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="filter_attribute_uuid", referencedColumnName="uuid")}
     *      )
     */
    private $filterAttributes;

    ...
}

class FilterAttribute {

        /**
     * @var int
     * @ORM\Column(type="smallint", options={"comment":"The position within the filter list"})
     */
    private $position;

    ...

}

And I have a method to get the filterAttributes as an ordered List in the Product Entity:

    /**
      * Return the FilterAttributes sorted by sorting order
      *
      * @return ArrayCollection|FilterAttribute[]
      */
     public function getFilterAttributesSorted()
     {
         $criteria = Criteria::create()
             ->orderBy(['position' => Criteria::ASC]);

         return $this->filterAttributes->matching($criteria);
     }

The Product Entity is loaded from the Database. So the Collection is a PersistentCollection with a ManyToManyPersister.

Expected Behavior: The returned list is ordered by the position field.
Current Behavior: The returned list is unordered.

Current Workaround:

    /**
      * Return the FilterAttributes sorted by sorting order
      *
      * @return ArrayCollection|FilterAttribute[]
      */
     public function getFilterAttributesSorted()
     {
         $criteria = Criteria::create()
             ->orderBy(['position' => Criteria::ASC]);

         $arrayCollection = $this->filterAttributes->matching($criteria);
         return $arrayCollection->matching($criteria);
     }
Originally created by @sven-communitales on GitHub (Jun 22, 2017). doctrine/orm: 2.5.6 The PersistentCollection::matching() method does not take the ordering of a Criteria in account. For an ArrayCollection is works fine. Reproduce: I have two Entities class Product { /** * @var FilterAttribute[]|ArrayCollection * @ORM\ManyToMany(targetEntity="FilterAttribute") * @ORM\JoinTable(name="product_filter_attribute", * joinColumns={@ORM\JoinColumn(name="product_uuid", referencedColumnName="uuid")}, * inverseJoinColumns={@ORM\JoinColumn(name="filter_attribute_uuid", referencedColumnName="uuid")} * ) */ private $filterAttributes; ... } class FilterAttribute { /** * @var int * @ORM\Column(type="smallint", options={"comment":"The position within the filter list"}) */ private $position; ... } And I have a method to get the filterAttributes as an ordered List in the Product Entity: /** * Return the FilterAttributes sorted by sorting order * * @return ArrayCollection|FilterAttribute[] */ public function getFilterAttributesSorted() { $criteria = Criteria::create() ->orderBy(['position' => Criteria::ASC]); return $this->filterAttributes->matching($criteria); } The Product Entity is loaded from the Database. So the Collection is a PersistentCollection with a ManyToManyPersister. Expected Behavior: The returned list is ordered by the position field. Current Behavior: The returned list is unordered. Current Workaround: /** * Return the FilterAttributes sorted by sorting order * * @return ArrayCollection|FilterAttribute[] */ public function getFilterAttributesSorted() { $criteria = Criteria::create() ->orderBy(['position' => Criteria::ASC]); $arrayCollection = $this->filterAttributes->matching($criteria); return $arrayCollection->matching($criteria); }
admin added the BugMissing Tests labels 2026-01-22 15:11:58 +01:00
Author
Owner

@lcobucci commented on GitHub (Jun 23, 2017):

@sven-communitales could you please send us a PR with a functional test that reproduces this behaviour? Sending just an example doesn't give us the complete overview of things and why it fails. You can find examples on 971c400025/tests/Doctrine/Tests/ORM/Functional

@lcobucci commented on GitHub (Jun 23, 2017): @sven-communitales could you please send us a PR with a functional test that reproduces this behaviour? Sending just an example doesn't give us the complete overview of things and why it fails. You can find examples on https://github.com/doctrine/doctrine2/tree/971c40002522cfebe58d80aff21eef9fe439fa60/tests/Doctrine/Tests/ORM/Functional
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5584