Symfony2 - Doctrine - From SQL to DQL

匿名 (未验证) 提交于 2019-12-03 01:45:01

问题:

I'm working on PHP project based on Symfony2 and I want this below to works on Symfony2 by Doctrine but actually I get this error by using the "...FROM xx JOIN (SELECT...." :

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

The final DQL statement that I want to make work is :

return $this->getEntityManager()                 ->createQuery(             "SELECT u.username, m1.id, n.id as notification, n.vue, u.lastActivity, LEFT(m1.message,60) as Message, m1.lu, m1.dateEnvoi, i.image             FROM MCoreBundle:Messages m1              JOIN (                 SELECT m.from, max(dateEnvoi) as dateEnvoi                  FROM MCoreBundle:Messages m                 JOIN MCoreBundle:User u on m.from = u.id                 WHERE m.to = :user                 GROUP BY m.from             ) t ON m1.from = t.from AND              m1.dateEnvoi = t.dateEnvoi,              MCoreBundle:User u,              MCoreBundle:Notification n,              MCoreBundle:Images i WHERE                    u.id = m.from AND                    n.id = m.notification AND                    i.user = u.id")                 ->setParameters(array('user' => $iIDUser))                 ->getResult(); 

Actual problem : Doctrine/Symfony doesnt recorgnize the "...FROM xx JOIN (SELECT...."

回答1:

try this

SELECT u.username, m1.id, n.id as notification, n.vue, u.lastActivity, LEFT(m1.message,60) as Message, m1.lu, m1.dateEnvoi, i.image FROM MCoreBundle:Messages m1, MCoreBundle:User u,  MCoreBundle:Notification n,  MCoreBundle:Images i  JOIN (   SELECT m.from, max(dateEnvoi) as dateEnvoi    FROM MCoreBundle:Messages m   JOIN MCoreBundle:User u on m.from = u.id   WHERE m.to = :user   GROUP BY m.from ) t ON m1.from = t.from AND m1.dateEnvoi = t.dateEnvoi WHERE u.id = m.from AND n.id = m.notification AND i.user = u.id 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!