Skip to content

Commit

Permalink
Merge pull request #7317 from protecinnovations/fix/7316-xml-order-by…
Browse files Browse the repository at this point in the history
…-dir-many-to-many

[XML] Fix default value of many-to-many order-by to ASC
  • Loading branch information
lcobucci authored Nov 20, 2018
2 parents ac50539 + 72121c0 commit f80656c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,9 @@ public function loadMetadataForClass($className, ClassMetadata $metadata)
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;
}
Expand Down
15 changes: 15 additions & 0 deletions tests/Doctrine/Tests/Models/GH7316/GH7316Article.php
Original file line number Diff line number Diff line change
@@ -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();
}
}
15 changes: 15 additions & 0 deletions tests/Doctrine/Tests/ORM/Mapping/XmlMappingDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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;

Expand Down Expand Up @@ -194,6 +195,20 @@ public function testOneToManyDefaultOrderByAsc()
);
}

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
Expand Down
Original file line number Diff line number Diff line change
@@ -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>

0 comments on commit f80656c

Please sign in to comment.