Doctrine 2不能识别FROM子句上的SELECT [英] Doctrine 2 does not recognize SELECT on the FROM clause

查看:190
本文介绍了Doctrine 2不能识别FROM子句上的SELECT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SQL查询我想在Doctrine2中使用。



我的查询,转到dabatase,执行 GROUP BY 用户状态和 COUNT 每个州的用户数。



然后我正在尝试加入一个表格到 COUNT 所有用户,并为每个州的百分比。

  return $ this-> getEntityManager()
- > createQuery(
SELECT COUNT(u.id)as total,
(100 *(COUNT(u.id) / total_users.total))as percent
FROM UserBundle:User u,
(SELECT COUNT(*)as total
FROM UserBundle:User)as total_users
LEFT JOIN u.idUserEstado ue
GROUP BY u.idUserEstado)
- > getResult();

问题是,当我运行Doctrine2查询时,我收到一个例外:

  [语义错误]行0,col 397附近
'(SELECT COUNT(':Error:Class'('not defined。

学说不认识 SELECT code> FROM 子句。

解决方案

选择on子句不由原则处理2



关于原则的jira有一个封闭的(不被接受的)功能要求: http://www.doctrine-project.org/jira/browse/DDC-2793


DQL是关于查询对象的。在FROM
子句中支持子选项意味着DQL解析器不能再构建结果集
映射(由子查询返回的字段可能不符合
对象了)这就是为什么它不能支持(支持它
只有在您运行查询而不进行水化的情况下,不需要
IMO,因为这意味着查询解析需要依赖于
执行模式) 。



I have a SQL query I would like to use in Doctrine2.

My query, goes to the dabatase, does a GROUP BY user state and COUNT the number of users per state.

Then I'm trying to join a table to COUNT all users and make a percent for every state.

return $this->getEntityManager()
            ->createQuery("
                SELECT COUNT(u.id) as total, 
                    (100*(COUNT( u.id ) /  total_users.total)) as percent
                FROM UserBundle:User u,
                    (SELECT COUNT(*) as total
                     FROM UserBundle:User) as total_users
                LEFT JOIN u.idUserEstado ue
                GROUP BY u.idUserEstado")
            ->getResult();

The problem is, when I run the Doctrine2 query I get an exception:

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

Doctrine does not recognize that SELECT on the FROM clause.

解决方案

Select on the from clause are not handled by doctrine 2

There is a closed (and not accepted) feature request on doctrine's jira : http://www.doctrine-project.org/jira/browse/DDC-2793

DQL is about querying objects. Supporting subselects in the FROM clause means that the DQL parser is not able to build the result set mapping anymore (as the fields returned by the subquery may not match the object anymore). This is why it cannot be supported (supporting it only for the case you run the query without the hydration is a no-go IMO as it would mean that the query parsing needs to be dependant of the execution mode).

这篇关于Doctrine 2不能识别FROM子句上的SELECT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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