diff --git a/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php b/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php index 3229b6b73..93b697cae 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php @@ -549,7 +549,9 @@ class XmlDriver extends FileDriver if (isset($manyToManyElement->{'order-by'})) { $orderBy = []; foreach ($manyToManyElement->{'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/GH7316/GH7316Article.php b/tests/Doctrine/Tests/Models/GH7316/GH7316Article.php new file mode 100644 index 000000000..a832cd70d --- /dev/null +++ b/tests/Doctrine/Tests/Models/GH7316/GH7316Article.php @@ -0,0 +1,15 @@ +<?php + +namespace Doctrine\Tests\Models\GH7316; + +use Doctrine\Common\Collections\ArrayCollection; + +class GH7316Article +{ + private $tags; + + public function __construct() + { + $this->tags = new ArrayCollection(); + } +} diff --git a/tests/Doctrine/Tests/ORM/Mapping/XmlMappingDriverTest.php b/tests/Doctrine/Tests/ORM/Mapping/XmlMappingDriverTest.php index 45beca8d4..8ff6c0183 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/XmlMappingDriverTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/XmlMappingDriverTest.php @@ -13,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\GH7316\GH7316Article; use Doctrine\Tests\Models\ValueObjects\Name; use Doctrine\Tests\Models\ValueObjects\Person; @@ -194,6 +195,20 @@ class XmlMappingDriverTest extends AbstractMappingDriverTest ); } + public function testManyToManyDefaultOrderByAsc() : void + { + $class = new ClassMetadata(GH7316Article::class); + $class->initializeReflection(new RuntimeReflectionService()); + + $driver = $this->_loadDriver(); + $driver->loadMetadataForClass(GH7316Article::class, $class); + + self::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.GH7316.GH7316Article.dcm.xml b/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.GH7316.GH7316Article.dcm.xml new file mode 100644 index 000000000..3820cdc9b --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.GH7316.GH7316Article.dcm.xml @@ -0,0 +1,14 @@ +<?xml version="1.0" encoding="UTF-8"?> +<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping + http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd"> + + <entity name="Doctrine\Tests\Models\GH7316\GH7316Article"> + <many-to-many field="tags" target-entity="NoTargetEntity" mapped-by="noMappedByField"> + <order-by> + <order-by-field name="position"/> + </order-by> + </many-to-many> + </entity> +</doctrine-mapping>