From ce9d4c739d939a2ed7583baeb1026b126ee51826 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 | 5 ++++- .../Tests/Models/GH7141/GH7141Article.php | 17 +++++++++++++++++ .../Tests/ORM/Mapping/XmlMappingDriverTest.php | 15 +++++++++++++++ ...ne.Tests.Models.GH7141.GH7141Article.dcm.xml | 14 ++++++++++++++ 4 files changed, 50 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 a85a65eab67..76077a85c5d 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php @@ -4,6 +4,7 @@ namespace Doctrine\ORM\Mapping\Driver; +use Doctrine\Common\Collections\Criteria; use Doctrine\DBAL\Types\Type; use Doctrine\ORM\Events; use Doctrine\ORM\Mapping; @@ -375,7 +376,9 @@ public function loadMetadataForClass( $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; } $association->setOrderBy($orderBy); diff --git a/tests/Doctrine/Tests/Models/GH7141/GH7141Article.php b/tests/Doctrine/Tests/Models/GH7141/GH7141Article.php new file mode 100644 index 00000000000..f02941feef8 --- /dev/null +++ b/tests/Doctrine/Tests/Models/GH7141/GH7141Article.php @@ -0,0 +1,17 @@ +tags = new ArrayCollection(); + } +} diff --git a/tests/Doctrine/Tests/ORM/Mapping/XmlMappingDriverTest.php b/tests/Doctrine/Tests/ORM/Mapping/XmlMappingDriverTest.php index 7da4de91b14..b22d63ebc5e 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/XmlMappingDriverTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/XmlMappingDriverTest.php @@ -4,6 +4,7 @@ namespace Doctrine\Tests\ORM\Mapping; +use Doctrine\Common\Collections\Criteria; use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\Mapping\ClassMetadataFactory; use Doctrine\ORM\Mapping\Driver\XmlDriver; @@ -12,6 +13,7 @@ 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; use DOMDocument; @@ -193,6 +195,19 @@ public static function dataValidSchema() }, $list); } + /** + * @group GH-7141 + */ + public function testOneToManyDefaultOrderByAsc() + { + $class = $this->createClassMetadata(GH7141Article::class); + + $this->assertEquals( + Criteria::ASC, + $class->getProperty('tags')->getOrderBy()['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 00000000000..e073f380f5f --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.GH7141.GH7141Article.dcm.xml @@ -0,0 +1,14 @@ + + + + + + + + + + +