Skip to content

Commit

Permalink
Fix default value of one-to-many order-by to ASC, #7141
Browse files Browse the repository at this point in the history
  • Loading branch information
Awkan authored and lcobucci committed Jun 17, 2019
1 parent 4ce24a4 commit ce9d4c7
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions tests/Doctrine/Tests/Models/GH7141/GH7141Article.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\GH7141;

use Doctrine\Common\Collections\ArrayCollection;

class GH7141Article
{
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 @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
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\GH7141\GH7141Article">
<one-to-many field="tags" target-entity="NoTargetEntity" mapped-by="noMappedByField">
<order-by>
<order-by-field name="position"/>
</order-by>
</one-to-many>
</entity>
</doctrine-mapping>

0 comments on commit ce9d4c7

Please sign in to comment.