Doctrine ORM order by annotations object fields

2019-07-12 01:53发布

So its simple like this : I have the following code in some Entity

     /**
     * @var ServiceOffer
     *
     * @ORM\OneToMany(targetEntity="ServiceOffer", mappedBy="serviceProvider")
     * @ORM\OrderBy({"service" = "desc"})
     */
    private $offers;

What i need is to do something like this

     /**
     * @var ServiceOffer
     *
     * @ORM\OneToMany(targetEntity="ServiceOffer", mappedBy="serviceProvider")
     * @ORM\OrderBy({"service.points" = "desc"})
     */
    private $offers;

which is not working i don't want to do it through some function i wanted straight forward with annotations is there any way to do this ?

this is the points btw:

    /**
     * @var integer
     *
     * @ORM\Column(name="points", type="integer", nullable=true)
     */
    private $points;

1条回答
该账号已被封号
2楼-- · 2019-07-12 02:21

No. This is not possible. You should solve this creating a dql.

The DQL Snippet in OrderBy is only allowed to consist of unqualified, unquoted field names and of an optional ASC/DESC positional statement. Multiple Fields are separated by a comma (,). The referenced field names have to exist on the targetEntity class of the @ManyToMany or @OneToMany annotation.

More in doctrine2 documentation.

DQL Example

SELECT provider, offer, service
  FROM \MyNamespace\Entity\ServiceProvider provider
  INNER JOIN provider.offers offer
  INNER JOIN offer.service service
WHERE 
  provider.id = 1

Notice, this code snippet provider, offer, service it's important to return here all entities or fields you will need so doctrine will load it for once, if it's possible. In other words, if you did'nt include any entity and call for it, doctrine will load it lazily.

查看更多
登录 后发表回答