Doctrine ORM 按注释对象字段排序 [英] Doctrine ORM order by annotations object fields

查看:28
本文介绍了Doctrine ORM 按注释对象字段排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以它很简单:我在某些实体中有以下代码

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;

我需要做这样的事情

     /**
     * @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 ?

顺便说一句:

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

推荐答案

没有.这不可能.您应该创建一个 dql 来解决这个问题.

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

OrderBy 中的 DQL 代码段只允许包含不合格、不带引号的字段名称和可选的 ASC/DESC 位置语句.多个字段由逗号 (,) 分隔.引用的字段名称必须存在于@ManyToMany 或@OneToMany 注释的targetEntity 类中.

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.

更多内容在 doctrine2 文档.

DQL 示例

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

注意,这个代码片段 provider, offer, service 在这里返回你需要的所有实体或字段很重要,所以如果可能的话,学说会加载它一次.换句话说,如果您没有包含任何实体并调用它,则准则将延迟加载它.

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.

这篇关于Doctrine ORM 按注释对象字段排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆