DDC-293: Patch for "orderBy" attribute for @ManyToMany #363

Open
opened 2026-01-22 12:36:06 +01:00 by admin · 0 comments
Owner

Originally created by @doctrinebot on GitHub (Jan 31, 2010).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user @beberlei:

There are lots of scenarios where a many to many collection has semantics in regard to the order of the entities. Here is a prove of concept patch that implementing an attribute "orderBy" for the ManyToMany annotation. The same can be implemented for OneToMany using the same pattern easily. Here are some excerpts from the test-case:

class RoutingRoute
{
    /****
     * @Id
     * @generatedValue(strategy="AUTO")
     * @column(type="integer")
     */
    public $id;

    /****
     * @ManyToMany(targetEntity="Doctrine\Tests\Models\Routing\RoutingLeg", cascade={"all"}, orderBy="departureDate ASC")
     * @JoinTable(name="RoutingRouteLegs",
     *     joinColumns={@JoinColumn(name="route_id", referencedColumnName="id")},
     *     inverseJoinColumns={@JoinColumn(name="leg_id", referencedColumnName="id", unique=true)}
     * )
     */
    public $legs;
    public function testOrderedCollection()
    {
        $route = new RoutingRoute();

        $leg1 = new RoutingLeg();
        $leg1->fromLocation = $this->locations['Berlin'];
        $leg1->toLocation   = $this->locations['Bonn'];
        $leg1->departureDate = new \DateTime("now");
        $leg1->arrivalDate = new \DateTime("now <ins>5 hours");

        $leg2 = new RoutingLeg();
        $leg2->fromLocation = $this->locations['Bonn'];
        $leg2->toLocation   = $this->locations['Brasilia'];
        $leg2->departureDate = new \DateTime("now </ins>6 hours");
        $leg2->arrivalDate = new \DateTime("now +24 hours");

        $route->legs[] = $leg2;
        $route->legs[] = $leg1;

        $this->_em->persist($route);
        $this->_em->flush();
        $routeId = $route->id;
        $this->_em->clear();

        $route = $this->_em->find('Doctrine\Tests\Models\Routing\RoutingRoute', $routeId);

        $this->assertEquals(2, count($route->legs));
        $this->assertEquals("Berlin", $route->legs[0]->fromLocation->getName());
        $this->assertEquals("Bonn", $route->legs[1]->fromLocation->getName());
    }
Originally created by @doctrinebot on GitHub (Jan 31, 2010). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user @beberlei: There are lots of scenarios where a many to many collection has semantics in regard to the order of the entities. Here is a prove of concept patch that implementing an attribute "orderBy" for the ManyToMany annotation. The same can be implemented for OneToMany using the same pattern easily. Here are some excerpts from the test-case: ``` class RoutingRoute { /**** * @Id * @generatedValue(strategy="AUTO") * @column(type="integer") */ public $id; /**** * @ManyToMany(targetEntity="Doctrine\Tests\Models\Routing\RoutingLeg", cascade={"all"}, orderBy="departureDate ASC") * @JoinTable(name="RoutingRouteLegs", * joinColumns={@JoinColumn(name="route_id", referencedColumnName="id")}, * inverseJoinColumns={@JoinColumn(name="leg_id", referencedColumnName="id", unique=true)} * ) */ public $legs; ``` ``` public function testOrderedCollection() { $route = new RoutingRoute(); $leg1 = new RoutingLeg(); $leg1->fromLocation = $this->locations['Berlin']; $leg1->toLocation = $this->locations['Bonn']; $leg1->departureDate = new \DateTime("now"); $leg1->arrivalDate = new \DateTime("now <ins>5 hours"); $leg2 = new RoutingLeg(); $leg2->fromLocation = $this->locations['Bonn']; $leg2->toLocation = $this->locations['Brasilia']; $leg2->departureDate = new \DateTime("now </ins>6 hours"); $leg2->arrivalDate = new \DateTime("now +24 hours"); $route->legs[] = $leg2; $route->legs[] = $leg1; $this->_em->persist($route); $this->_em->flush(); $routeId = $route->id; $this->_em->clear(); $route = $this->_em->find('Doctrine\Tests\Models\Routing\RoutingRoute', $routeId); $this->assertEquals(2, count($route->legs)); $this->assertEquals("Berlin", $route->legs[0]->fromLocation->getName()); $this->assertEquals("Bonn", $route->legs[1]->fromLocation->getName()); } ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#363