Doctrine 2 DQL子查询

时间:2013-08-29 08:30:59

标签: symfony doctrine-orm doctrine dql

我正在尝试通过Doctrine 2.3 DQL在FROM Block中使用子查询。

每次运行此操作时,都会出现以下错误

[Semantical Error] line 0, col 839 near '(SELECT ': Error: Class '(' is not defined.

这是我目前的DQL代码

$query = $this->getEntityManager()->createQuery('
SELECT 
        taS2.netDueDate,
        SUM(taS2.amountInThousands)                     AS SumAmountInThousands,
        SUM(taS2.netDAO1)                                       AS SumnetDAO1,
        SUM(taS2.netDAO2)                                               AS SumnetDAO2,
        SUM(taS2.netDAO3)                                           AS SumnetDAO3,
        SUM(taS2.netDAO4)                                               AS SumnetDAO4,
        SUM(taS2.netDueDate * taS2.amountInThousands)   AS SumNetDueDateInThousands,
        SUM(taS2.critical)  
FROM 
    (SELECT 
        ta.id,
        ta.documentDate,
        SUBSTRING(CONCAT(ta.netDueDate, \'\'), 9, 2) AS netDueDate,
        ta.discountRate, 
        ta.amountInThousands, 
        ta.netDAO1, 
        ta.netDAO2, 
        ta.netDAO3, 
        ta.netDAO4,
        ta.netDueDate,
        1 AS counter,
        CASE 
             WHEN (ta.clearingDate > ta.netDueDate) AND 
                     SUBSTRING(CONCAT(ta.clearingDate, \'\'), 6, 2) <> SUBSTRING(CONCAT(ta.netDueDate, \'\'), 6, 2)
             THEN ta.amountInThousands ELSE 0
        END AS critical
    FROM 
        AcmeBundle:Transaction ta
    WHERE
        ta.discountRate = 0) taS2

GROUP BY taS2.netDueDate
ORDER BY SUM(taS2.amountInThousands) DESC;
);

有人知道我怎么做到这一点吗?

1 个答案:

答案 0 :(得分:-2)

您的查询看起来更像是具有SQL个功能的普通DQL。您可以使用$this->getEntityManager()->createNativeQuery();

执行SQL查询

另请阅读Doctrine2 Subqueries