From 2560d4f419ad51869abd07f047cdda454f3fdc7e Mon Sep 17 00:00:00 2001 From: Donovan Bourlard Date: Thu, 22 Mar 2018 14:51:02 +0100 Subject: [PATCH] Fix default value of one-to-many order-by to ASC, #7141 --- lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php | 6 +++++- .../Tests/Models/GH7141/GH7141Article.php | 15 ++++++++++++++ .../ORM/Mapping/XmlMappingDriverTest.php | 20 +++++++++++++++++++ ....Tests.Models.GH7141.GH7141Article.dcm.xml | 14 +++++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 tests/Doctrine/Tests/Models/GH7141/GH7141Article.php create mode 100644 tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.GH7141.GH7141Article.dcm.xml diff --git a/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php b/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php index ca2707278..3229b6b73 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php @@ -19,6 +19,7 @@ namespace Doctrine\ORM\Mapping\Driver; +use Doctrine\Common\Collections\Criteria; use SimpleXMLElement; use Doctrine\Common\Persistence\Mapping\Driver\FileDriver; use Doctrine\ORM\Mapping\Builder\EntityListenerBuilder; @@ -429,7 +430,10 @@ class XmlDriver extends FileDriver if (isset($oneToManyElement->{'order-by'})) { $orderBy = []; foreach ($oneToManyElement->{'order-by'}->{'order-by-field'} as $orderByField) { - $orderBy[(string) $orderByField['name']] = (string) $orderByField['direction']; + $orderBy[(string) $orderByField['name']] = isset($orderByField['direction']) + ? (string) $orderByField['direction'] + : Criteria::ASC + ; } $mapping['orderBy'] = $orderBy; } diff --git a/tests/Doctrine/Tests/Models/GH7141/GH7141Article.php b/tests/Doctrine/Tests/Models/GH7141/GH7141Article.php new file mode 100644 index 000000000..5af97c62e --- /dev/null +++ b/tests/Doctrine/Tests/Models/GH7141/GH7141Article.php @@ -0,0 +1,15 @@ +tags = new ArrayCollection(); + } +} diff --git a/tests/Doctrine/Tests/ORM/Mapping/XmlMappingDriverTest.php b/tests/Doctrine/Tests/ORM/Mapping/XmlMappingDriverTest.php index cc543b891..d0a38b490 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/XmlMappingDriverTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/XmlMappingDriverTest.php @@ -2,6 +2,7 @@ namespace Doctrine\Tests\ORM\Mapping; +use Doctrine\Common\Collections\Criteria; use Doctrine\Common\Persistence\Mapping\RuntimeReflectionService; use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\Mapping\ClassMetadataFactory; @@ -11,6 +12,7 @@ use Doctrine\Tests\Models\DDC3293\DDC3293User; use Doctrine\Tests\Models\DDC3293\DDC3293UserPrefixed; use Doctrine\Tests\Models\DDC889\DDC889Class; use Doctrine\Tests\Models\Generic\SerializationModel; +use Doctrine\Tests\Models\GH7141\GH7141Article; use Doctrine\Tests\Models\ValueObjects\Name; use Doctrine\Tests\Models\ValueObjects\Person; @@ -174,6 +176,24 @@ class XmlMappingDriverTest extends AbstractMappingDriverTest }, $list); } + /** + * @group GH-7141 + */ + public function testOneToManyDefaultOrderByAsc() + { + $driver = $this->_loadDriver(); + $class = new ClassMetadata(GH7141Article::class); + + $class->initializeReflection(new RuntimeReflectionService()); + $driver->loadMetadataForClass(GH7141Article::class, $class); + + + $this->assertEquals( + Criteria::ASC, + $class->getMetadataValue('associationMappings')['tags']['orderBy']['position'] + ); + } + /** * @group DDC-889 * @expectedException \Doctrine\Common\Persistence\Mapping\MappingException diff --git a/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.GH7141.GH7141Article.dcm.xml b/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.GH7141.GH7141Article.dcm.xml new file mode 100644 index 000000000..e073f380f --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.GH7141.GH7141Article.dcm.xml @@ -0,0 +1,14 @@ + + + + + + + + + + +