本文介绍了原则2:如果没有选择至少一个根实体别名,则无法通过标识变量选择实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是我的实际问题,而是对我自己的便笺,可能会对他人有所帮助。还有许多其他类似的问题:,,,,,,但似乎没有一个提供此解决方案。

Rather than an actual question, this is a sticky note for myself, that may help others. There are many other similar questions: 1, 2, 3, 4, 5, 6, but none of them seems to offer this solution.

我有以下实体:

class Order
{
    // ...

    /**
     * @ManyToOne(targetEntity="Customer")
     * @var Customer
     */
    private $customer;

    /**
     * @Column(type="integer")
     * @var int
     */
    private $amount;
}

class Customer
{
    // ...
}

Order Customer 。我想获得每个客户及其订单总额,所以我运行以下DQL查询:

Order has a unidirectional, many-to-one relationship with Customer. I want to get every customer, along with the total amount of his orders, so I run the following DQL query:

SELECT c, SUM(o.amount)
FROM Model\Order o
JOIN o.customer c
GROUP BY c

但出现以下错误:

我该如何解决?

推荐答案

这是一个已知的原则限制。

This is a known Doctrine limitation.

解决方案是显式地 SELECT 您想要的实体检索( Customer )并使用从那里手动加入另一个实体( Order )有条件:

The solution is to explicitly SELECT the entity you want to retrieve (Customer) and manually join the other entity (Order) from there, using a WITH condition:

SELECT c, SUM(o.amount)
FROM Model\Customer c
JOIN Model\Order o WITH o.customer = c
GROUP BY c

这篇关于原则2:如果没有选择至少一个根实体别名,则无法通过标识变量选择实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 22:16