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
the
is strange. Maybe it can't find the class User.
COUNT(*)
may not exists in DQL. TryCOUNT(u.id)
, the results will be the same. You also have the possibility to try this in an EntityRepository :Source: Google UserGroup issue