Details
-
Type:
Sub-task
-
Status:
Resolved
-
Priority:
Minor
-
Resolution: Invalid
-
Affects Version/s: 2.0-ALPHA4
-
Fix Version/s: None
-
Component/s: ORM
-
Security Level: All
-
Labels:None
Description
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 +5 hours"); $leg2 = new RoutingLeg(); $leg2->fromLocation = $this->locations['Bonn']; $leg2->toLocation = $this->locations['Brasilia']; $leg2->departureDate = new \DateTime("now +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()); }
Issue Links
- is referenced by
-
DDC-195
Ordering of associations
-
Discussion in IRC had the following results:
1. @orderBy("b.start_year ASC, b.end_year DESC") 2. SELECT a, b FROM a JOIN b ORDER BY b.startYear DESC 1+2 => ORDER BY b.start_year DESC, b.start_year ASC, b.end_year DESC