Symfony DQL语法错误

时间:2015-11-30 20:17:37

标签: symfony doctrine dql

我尝试进行DQL查询,但遇到了一些麻烦...

$user = $this->getUser();

$query = $em->createQuery(
    'SELECT p
       FROM AppBundle:User u
         JOIN AppBundle:Follower f
         JOIN AppBundle:Photo p 
           WHERE u.id = :id
           AND f.follower_id = :id
           AND p.user_id = f.user_id'
)->setParameter('id', $user->getId());

我正在尝试获取这些用户的照片(AppBundle:Photo),已登录用户正在关注这些照片。

获取下一个错误:

`[Syntax Error] line 0, col 128: Error: Expected =, <, <=, <>, >, >=, !=, got 'p'`

这里的错误与&#39; p&#39; ?

2 个答案:

答案 0 :(得分:1)

我不明白为什么follower在您的代码中,我看不到与photo的关系...

之后,我想你打电话给Join,但我没有看到你与照片的关系......

$query = $em->createQuery(
'SELECT p
  FROM AppBundle:Photo p       
     JOIN p.user u
       WHERE u.id = :id ')->setParameter('id', $user->getId());

以下是official doc的一部分: 例如:

  

定期加入地址:

     

createQuery(“SELECT u FROM User u JOIN u.address a   WHERE a.city ='Berlin'“); $ users = $ query-&gt; getResult();

     

获取地址的连接:

     

createQuery(“SELECT u,FROM User u JOIN   u.address a WHERE a.city ='Berlin'“); $ users = $ query-&gt; getResult();

答案 1 :(得分:0)

你试试这个吗?:

$query = $em->createQuery(
'SELECT p
  FROM AppBundle:Photo p       
     JOIN AppBundle:Follower f
     JOIN  AppBundle:User u
       WHERE u.id = :id
       AND f.follower_id = :id
       AND p.user_id = f.user_id' )->setParameter('id', $user->getId());