user1236048
user1236048

Reputation: 5602

Order Association by other Association from Mapping - Doctrine2

This works Ordering To-Many Associations

/** @Entity **/
class User
{
    // ...

    /**
     * @ORM\ManyToMany(targetEntity="Group")
     * @ORM\OrderBy({"name" = "ASC"})
     **/
    private $groups;
}

But if in my Group Entity I have an To-One association (therefore a foreign_key field), I can't order by foreign_key field:

/** @Entity **/
class Group
{
    // ...

    /**
     * @ORM\ManyToOne(targetEntity="Auxiliar", inversedBy="groups", fetch="EAGER")
     * @ORM\JoinColumn(name="auxiliar_id", referencedColumnName="id")
     **/
    private $auxiliar;
}

Why can't I order the groups by auxiliar_id? This doesn't work (500: unrecognized field):

/** @Entity **/
class User
{
    // ...

    /**
     * @ORM\ManyToMany(targetEntity="Group")
     * @ORM\OrderBy({"auxiliar_id" = "ASC"})
     **/
    private $groups;
}

EDIT:

Also tried: auxiliar, groups.auxiliar, groups.auxiliar_id, auxiliar.id

Upvotes: 1

Views: 579

Answers (1)

Vadim Ashikhman
Vadim Ashikhman

Reputation: 10126

You can add the auxiliar_id field inside the Group entity and it will be filled by doctrine after hydration.

Upvotes: 1

Related Questions