XML one-to-many order-by default direction isn't set #5926

Closed
opened 2026-01-22 15:22:14 +01:00 by admin · 2 comments
Owner

Originally created by @Awkan on GitHub (Mar 20, 2018).

Originally assigned to: @Ocramius on GitHub.

Context

I have a oneToMany relation between 2 entities. For example an Article which has many tags.

class Article
{
    private $id;
    private $name;
    private $tags;

    public function __construct()
    {
        $this->tags = new ArrayCollection();
    }
}

class Tag
{
    private $id;
    private $name;
}

My mapping is declared as XML.

Then, I want to get my tags ordered by name. For that, I have the following mapping in my Article:

<entity name="Article">
    <!-- ... -->
            <one-to-many field="tags" target-entity="Tag">
            <order-by>
                <order-by-field name="name"/>
            </order-by>
        </one-to-many>
</entity>

Problem

By default, this should sort my name field ASC (as mentionned in XSD file). However I have the exception Invalid order by orientation specified for Tag#name

Way of solution

For the moment, adding explicitly my direction attribute works fine but the default value isn't very default

<entity name="Article">
    <!-- ... -->
            <one-to-many field="tags" target-entity="Tag">
            <order-by>
                <order-by-field name="name" direction="ASC"/>
            </order-by>
        </one-to-many>
</entity>

I've investigate about it and it's appear that during the mapping reading (https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php#L375), the default direction isn't set. Should be set it by default to this line ?

Originally created by @Awkan on GitHub (Mar 20, 2018). Originally assigned to: @Ocramius on GitHub. Context -------- I have a `oneToMany` relation between 2 entities. For example an Article which has many tags. ```php class Article { private $id; private $name; private $tags; public function __construct() { $this->tags = new ArrayCollection(); } } class Tag { private $id; private $name; } ``` My mapping is declared as XML. Then, I want to get my tags ordered by name. For that, I have the following mapping in my `Article`: ```xml <entity name="Article"> <!-- ... --> <one-to-many field="tags" target-entity="Tag"> <order-by> <order-by-field name="name"/> </order-by> </one-to-many> </entity> ``` Problem -------- By default, this should sort my name field ASC (as mentionned in XSD file). However I have the exception `Invalid order by orientation specified for Tag#name` Way of solution -------- For the moment, adding explicitly my direction attribute works fine but the default value isn't very default ```xml <entity name="Article"> <!-- ... --> <one-to-many field="tags" target-entity="Tag"> <order-by> <order-by-field name="name" direction="ASC"/> </order-by> </one-to-many> </entity> ``` I've investigate about it and it's appear that during the mapping reading (https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php#L375), the default direction isn't set. Should be set it by default to this line ?
admin added the Bug label 2026-01-22 15:22:14 +01:00
admin closed this issue 2026-01-22 15:22:14 +01:00
Author
Owner

@Ocramius commented on GitHub (Mar 20, 2018):

@Awkan setting the default direction (if not set) in the driver is likely the correct approach.

@Ocramius commented on GitHub (Mar 20, 2018): @Awkan setting the default direction (if not set) in the driver is likely the correct approach.
Author
Owner

@lcobucci commented on GitHub (Nov 20, 2018):

Fixed by https://github.com/doctrine/doctrine2/pull/7146

@lcobucci commented on GitHub (Nov 20, 2018): Fixed by https://github.com/doctrine/doctrine2/pull/7146
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5926