学说2案例陈述

时间:2014-09-18 20:15:39

标签: doctrine-orm dql

我使用Doctirine 2和zf2。我怎样才能将此sql查询转换为dql。 (Doctirine查询语言)

我的产品实体在这里“应用程序\实体\产品”我已经尝试过但它不起作用..

感谢您的帮助。

SELECT price_range, count(*) AS num
FROM
(SELECT CASE WHEN price >= 0 AND price <= 10 THEN '0-10'
             WHEN price >= 10 AND price <= 20 THEN '10-20'
             WHEN price >= 20 AND price <= 30 THEN '30-40'
             WHEN price >= 30 AND price <= 40 THEN '30-40'
             WHEN price >= 40 AND price <= 50 THEN '40-50'
             WHEN price >= 50 AND price <= 60 THEN '50-60'
             WHEN price >= 60 AND price <= 70 THEN '60-70'
             WHEN price >= 70 AND price <= 80 THEN '70-80'
             WHEN price >= 80 AND price <= 90 THEN '90-100'
             WHEN price >= 100 AND price <= 110 THEN '100-110'
             ELSE 'over 1000'
         END as price_range
FROM product
WHERE 1
) AS  price_summaries
GROUP BY price_range

2 个答案:

答案 0 :(得分:0)

嗯,不幸的是你做不到。 Doctrine不支持CASE。看看你的问题,我也无法用DQL找到足够的其他解决方案。

但您可以使用native query。本机查询是嵌入的原始SQL查询,基本上是PDO的扩展。

当然一般不鼓励本地查询,因为它们有两个缺点:

  1. 您必须使用ResultSetMapping自己对Doctrine实体进行补充(如果您希望查询结果包含现成实体而不是普通数组/对象)。
  2. 原生查询破坏了使用DBAL的全部要点:您的可移植性松散。 (但是,说实话,大多数项目不只是切换他们的RDBMS,所以这是一个相当理论上的问题。)
  3. 但是如果您需要执行上述查询,请在这种情况下使用原生查询

答案 1 :(得分:0)

Doctirine支持案例陈述。

这有效

SELECT CASE WHEN (p.price >= 0 AND p.price <= 10) THEN '0-10'
                             WHEN (p.price >= 10 AND p.price <= 20) THEN '10-20'
                             WHEN (p.price >= 20 AND p.price <= 30) THEN '30-40'
                             WHEN (p.price >= 30 AND p.price <= 40) THEN '30-40'
                             WHEN (p.price >= 40 AND p.price <= 50) THEN '40-50'
                             WHEN (p.price >= 50 AND p.price <= 60) THEN '50-60'
                             WHEN (p.price >= 60 AND p.price <= 70) THEN '60-70'
                             WHEN (p.price >= 70 AND p.price <= 80) THEN '70-80'
                             WHEN (p.price >= 80 AND p.price <= 90) THEN '90-100'
                             WHEN (p.price >= 100 AND p.price <= 110) THEN '100-110'
                             ELSE 'over 1000'
                         END as price_range
                        FROM \Application\Entity\Product p

但这不起作用,我想我应该尝试原生查询

SELECT price_range, count(*) AS num
                    FROM
                    (SELECT CASE WHEN (p.price >= 0 AND p.price <= 10) THEN '0-10'
                         WHEN (p.price >= 10 AND p.price <= 20) THEN '10-20'
                         WHEN (p.price >= 20 AND p.price <= 30) THEN '30-40'
                         WHEN (p.price >= 30 AND p.price <= 40) THEN '30-40'
                         WHEN (p.price >= 40 AND p.price <= 50) THEN '40-50'
                         WHEN (p.price >= 50 AND p.price <= 60) THEN '50-60'
                         WHEN (p.price >= 60 AND p.price <= 70) THEN '60-70'
                         WHEN (p.price >= 70 AND p.price <= 80) THEN '70-80'
                         WHEN (p.price >= 80 AND p.price <= 90) THEN '90-100'
                         WHEN (p.price >= 100 AND p.price <= 110) THEN '100-110'
                         ELSE 'over 1000'
                     END as price_range
                    FROM \Application\Entity\Product p
                    ) AS  price_summaries
                    GROUP BY price_range