DDC-3952: [GH-1535] Fix for many-to-many ignoring "order by" in Criteria #4832

Closed
opened 2026-01-22 14:50:13 +01:00 by admin · 4 comments
Owner

Originally created by @doctrinebot on GitHub (Oct 13, 2015).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user @doctrinebot:

This issue is created automatically through a Github pull request on behalf of SammyK:

Url: https://github.com/doctrine/doctrine2/pull/1535

Message:

The matching of a Criteria in a many-to-many association will ignore the "order by" clause.

use Doctrine\Common\Collections\Criteria;

/****
 * @Entity @Table(name="hospitals")
 ****/
class Hospital
{
    // . . . 

    /****
     * @ManyToMany(targetEntity="User")
     * @JoinTable(name="foo*join*table",
     *      joinColumns={@JoinColumn(name="hospital_id", referencedColumnName="id")},
     *      inverseJoinColumns={@JoinColumn(name="user_id", referencedColumnName="id")}
     *      )
     ****/
    protected $users;

    public function getUsers()
    {
        $criteria = Criteria::create()
            // . . .
            ->orderBy(array("name" => Criteria::ASC));

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

// . . .

$users = $entityManager->find(Hospital::class, $hospital_id)->getUsers();

// $users will not be sorted by "name"

This is a very crude fix. I just stated using doctrine this week so I'm sure there's a better/more secure way to fix it. But this hot fix got it working on my local installation. :)

Originally created by @doctrinebot on GitHub (Oct 13, 2015). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user @doctrinebot: This issue is created automatically through a Github pull request on behalf of SammyK: Url: https://github.com/doctrine/doctrine2/pull/1535 Message: The matching of a `Criteria` in a many-to-many association will ignore the "order by" clause. ``` php use Doctrine\Common\Collections\Criteria; /**** * @Entity @Table(name="hospitals") ****/ class Hospital { // . . . /**** * @ManyToMany(targetEntity="User") * @JoinTable(name="foo*join*table", * joinColumns={@JoinColumn(name="hospital_id", referencedColumnName="id")}, * inverseJoinColumns={@JoinColumn(name="user_id", referencedColumnName="id")} * ) ****/ protected $users; public function getUsers() { $criteria = Criteria::create() // . . . ->orderBy(array("name" => Criteria::ASC)); return $this->users->matching($criteria); } } // . . . $users = $entityManager->find(Hospital::class, $hospital_id)->getUsers(); // $users will not be sorted by "name" ``` This is a very crude fix. I just stated using doctrine this week so I'm sure there's a better/more secure way to fix it. But this hot fix got it working on my local installation. :)
admin added the Bug label 2026-01-22 14:50:13 +01:00
admin closed this issue 2026-01-22 14:50:14 +01:00
Author
Owner

@doctrinebot commented on GitHub (Oct 13, 2015):

Comment created by @doctrinebot:

A related Github Pull-Request [GH-1535] was labeled:
https://github.com/doctrine/doctrine2/pull/1535

@doctrinebot commented on GitHub (Oct 13, 2015): Comment created by @doctrinebot: A related Github Pull-Request [GH-1535] was labeled: https://github.com/doctrine/doctrine2/pull/1535
Author
Owner

@doctrinebot commented on GitHub (Oct 21, 2015):

Comment created by @doctrinebot:

A related Github Pull-Request [GH-1535] was unlabeled:
https://github.com/doctrine/doctrine2/pull/1535

@doctrinebot commented on GitHub (Oct 21, 2015): Comment created by @doctrinebot: A related Github Pull-Request [GH-1535] was unlabeled: https://github.com/doctrine/doctrine2/pull/1535
Author
Owner

@doctrinebot commented on GitHub (Oct 21, 2015):

Comment created by @doctrinebot:

A related Github Pull-Request [GH-1535] was labeled:
https://github.com/doctrine/doctrine2/pull/1535

@doctrinebot commented on GitHub (Oct 21, 2015): Comment created by @doctrinebot: A related Github Pull-Request [GH-1535] was labeled: https://github.com/doctrine/doctrine2/pull/1535
Author
Owner

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

Might be releated and fixed by https://github.com/doctrine/orm/pull/5669

@beberlei commented on GitHub (Feb 16, 2020): Might be releated and fixed by https://github.com/doctrine/orm/pull/5669
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#4832