2014-01-19 131 views
0

我正在研究基於Symfony2的PHP項目,我希望下面的這個教程能夠在Symfony2上工作,但實際上我通過使用「... FROM xx JOIN(SELECT .... 「:Symfony2 - 學說 - 從SQL到DQL

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

,我想使工作的最後DQL語句是:

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(); 

實際的問題: 主義/ Symfony的犯規recorgnize的」 ... FROM XX加入(選擇....「

+0

你加入MCoreBundle:用戶U,MCoreBundle:通知N,MCoreBundle:圖片我。如果是的話,你必須在每次加入表格時寫入加入。 – sonam

+0

嘗試過,但它並沒有真正解決我的問題,仍然得到相同的錯誤...'(SELECT':錯誤:類'('未定義。 –

回答

0

試試這個

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 
+0

仍然,同樣的問題,Symfony不喜歡「JOIN(... 「部分:/ –