From eb00a3b81738a860b484b807e9ad3b6cf9f2cda8 Mon Sep 17 00:00:00 2001 From: beberlei Date: Fri, 26 Feb 2010 21:26:06 +0000 Subject: [PATCH] [2.0] DDC-195 - Refactored Mapping Syntax again to be arrays in all annotations, xml and yaml drivers instead of parsing the fields in the AssociationMapping implementations. --- lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php | 16 ++++++++++++---- lib/Doctrine/ORM/Mapping/ManyToManyMapping.php | 14 +++----------- lib/Doctrine/ORM/Mapping/OneToManyMapping.php | 14 +++----------- .../Tests/Models/Routing/RoutingRoute.php | 4 ++-- ...deredJoinedTableInheritanceCollectionTest.php | 4 ++-- .../ORM/Mapping/AbstractMappingDriverTest.php | 2 +- .../xml/Doctrine.Tests.ORM.Mapping.User.dcm.xml | 5 ++++- .../yaml/Doctrine.Tests.ORM.Mapping.User.dcm.yml | 3 ++- 8 files changed, 29 insertions(+), 33 deletions(-) diff --git a/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php b/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php index a9f985661..5232791a0 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php @@ -272,8 +272,12 @@ class XmlDriver extends AbstractFileDriver $mapping['orphanRemoval'] = (bool)$oneToManyElement->{'orphan-removal'}; } - if (isset($oneToManyElement['order-by'])) { - $mapping['orderBy'] = (string)$oneToManyElement['order-by']; + if (isset($oneToManyElement->{'order-by'})) { + $orderBy = array(); + foreach ($oneToManyElement->{'order-by'}->{'order-by-field'} AS $orderByField) { + $orderBy[(string)$orderByField['name']] = (string)$orderByField['direction']; + } + $mapping['orderBy'] = $orderBy; } $metadata->mapOneToMany($mapping); @@ -363,8 +367,12 @@ class XmlDriver extends AbstractFileDriver $mapping['orphanRemoval'] = (bool)$manyToManyElement->{'orphan-removal'}; } - if (isset($manyToManyElement['order-by'])) { - $mapping['orderBy'] = (string)$manyToManyElement['order-by']; + if (isset($manyToManyElement->{'order-by'})) { + $orderBy = array(); + foreach ($manyToManyElement->{'order-by'}->{'order-by-field'} AS $orderByField) { + $orderBy[(string)$orderByField['name']] = (string)$orderByField['direction']; + } + $mapping['orderBy'] = $orderBy; } $metadata->mapManyToMany($mapping); diff --git a/lib/Doctrine/ORM/Mapping/ManyToManyMapping.php b/lib/Doctrine/ORM/Mapping/ManyToManyMapping.php index 8ebb1c0b9..af9fef5e3 100644 --- a/lib/Doctrine/ORM/Mapping/ManyToManyMapping.php +++ b/lib/Doctrine/ORM/Mapping/ManyToManyMapping.php @@ -146,18 +146,10 @@ class ManyToManyMapping extends AssociationMapping } if (isset($mapping['orderBy'])) { - $parts = explode(",", $mapping['orderBy']); - $orderByGroup = array(); - foreach ($parts AS $part) { - $orderByItem = explode(" ", trim($part)); - if (count($orderByItem) == 1) { - $orderByGroup[$orderByItem[0]] = "ASC"; - } else { - $orderByGroup[$orderByItem[0]] = array_pop($orderByItem); - } + if (!is_array($mapping['orderBy'])) { + throw new \InvalidArgumentException("'orderBy' is expected to be an array, not ".gettype($mapping['orderBy'])); } - - $this->orderBy = $orderByGroup; + $this->orderBy = $mapping['orderBy']; } } diff --git a/lib/Doctrine/ORM/Mapping/OneToManyMapping.php b/lib/Doctrine/ORM/Mapping/OneToManyMapping.php index 0de4dcdc7..6ee7de096 100644 --- a/lib/Doctrine/ORM/Mapping/OneToManyMapping.php +++ b/lib/Doctrine/ORM/Mapping/OneToManyMapping.php @@ -86,18 +86,10 @@ class OneToManyMapping extends AssociationMapping (bool) $mapping['orphanRemoval'] : false; if (isset($mapping['orderBy'])) { - $parts = explode(",", $mapping['orderBy']); - $orderByGroup = array(); - foreach ($parts AS $part) { - $orderByItem = explode(" ", trim($part)); - if (count($orderByItem) == 1) { - $orderByGroup[$orderByItem[0]] = "ASC"; - } else { - $orderByGroup[$orderByItem[0]] = array_pop($orderByItem); - } + if (!is_array($mapping['orderBy'])) { + throw new \InvalidArgumentException("'orderBy' is expected to be an array, not ".gettype($mapping['orderBy'])); } - - $this->orderBy = $orderByGroup; + $this->orderBy = $mapping['orderBy']; } } diff --git a/tests/Doctrine/Tests/Models/Routing/RoutingRoute.php b/tests/Doctrine/Tests/Models/Routing/RoutingRoute.php index c870f6ca9..0f0f671c1 100644 --- a/tests/Doctrine/Tests/Models/Routing/RoutingRoute.php +++ b/tests/Doctrine/Tests/Models/Routing/RoutingRoute.php @@ -22,13 +22,13 @@ class RoutingRoute * joinColumns={@JoinColumn(name="route_id", referencedColumnName="id")}, * inverseJoinColumns={@JoinColumn(name="leg_id", referencedColumnName="id", unique=true)} * ) - * @OrderBy("departureDate ASC") + * @OrderBy({"departureDate" = "ASC"}) */ public $legs; /** * @OneToMany(targetEntity="RoutingRouteBooking", mappedBy="route") - * @OrderBy("passengerName ASC") + * @OrderBy({"passengerName" = "ASC"}) */ public $bookings = array(); diff --git a/tests/Doctrine/Tests/ORM/Functional/OrderedJoinedTableInheritanceCollectionTest.php b/tests/Doctrine/Tests/ORM/Functional/OrderedJoinedTableInheritanceCollectionTest.php index 7a243903c..0112a86bc 100644 --- a/tests/Doctrine/Tests/ORM/Functional/OrderedJoinedTableInheritanceCollectionTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/OrderedJoinedTableInheritanceCollectionTest.php @@ -97,7 +97,7 @@ abstract class OJTIC_Pet /** * @OneToMany(targetEntity="OJTIC_Pet", mappedBy="mother") - * @OrderBy("name ASC") + * @OrderBy({"name" = "ASC"}) */ public $children; @@ -106,7 +106,7 @@ abstract class OJTIC_Pet * @JoinTable(name="OTJIC_Pet_Friends", * joinColumns={@JoinColumn(name="pet_id", referencedColumnName="id")}, * inverseJoinColumns={@JoinColumn(name="friend_id", referencedColumnName="id")}) - * @OrderBy("name ASC") + * @OrderBy({"name" = "ASC"}) */ public $friends; diff --git a/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php b/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php index a41c4a22a..8acbd60ca 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php @@ -207,7 +207,7 @@ class User /** * * @OneToMany(targetEntity="Phonenumber", mappedBy="user", cascade={"persist"}) - * @OrderBy("number ASC") + * @OrderBy({"number"="ASC"}) */ public $phonenumbers; diff --git a/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.User.dcm.xml b/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.User.dcm.xml index c28baa48e..44ea3ae95 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.User.dcm.xml +++ b/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.User.dcm.xml @@ -24,7 +24,10 @@ - + + + + diff --git a/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.ORM.Mapping.User.dcm.yml b/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.ORM.Mapping.User.dcm.yml index 2e9ed0302..d5f9447c6 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.ORM.Mapping.User.dcm.yml +++ b/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.ORM.Mapping.User.dcm.yml @@ -27,7 +27,8 @@ Doctrine\Tests\ORM\Mapping\User: phonenumbers: targetEntity: Phonenumber mappedBy: user - orderBy: number ASC + orderBy: + number: ASC cascade: [ persist ] manyToMany: groups: