多个连接不能与createQueryBuilder一起使用

时间:2014-01-25 13:01:25

标签: php symfony doctrine-orm

当我使用createQuery时,它可以工作:

$this->getEntityManager()
    ->createQuery(
        'SELECT e FROM CPBundle:Event e, CPBundle:Player p, CPBundle:EventType et
        WHERE e.player = p.id AND e.eventType = et.id';
    )
    ->getResult();

但是当我使用createQueryBuilder尝试相同的查询时,它不起作用:

public function findEventsByParams($params)
{
    $qb = $this->getEntityManager()->createQueryBuilder();

    $query = $qb->select('e', 'p', 'et')
        ->from('CPBundle:Event', 'e')
        ->innerJoin('CPBundle:Player', 'p')
        ->innerJoin('CPBundle:EventType', 'et')
        ->where('e.player = p.id')
        ->andWhere('e.eventType = et.id');

    if ($params['username']) {
        $qb->andWhere('p.username = :username')
            ->setParameter('username', $params['username']);
    }

    //TODO: other params

    return $query->getQuery()->getResult();
}

我收到此错误消息:

  

[语法错误]第0行,第73栏:错误:预期文字,得到'加入'

1 个答案:

答案 0 :(得分:1)

请参阅此答案:https://stackoverflow.com/questions/17989473/symfony2-doctrine-multiple-joins-returns-error

public function findEventsByParams($params)
{
    $qb = $this->getEntityManager()->createQueryBuilder();

    $query = $qb->select('e')
        ->from('CPBundle:Event', 'e')
        ->innerJoin('e.player', 'p')
        ->innerJoin('e.eventType', 'et');

    if ($params['username']) {
        $qb->andWhere('p.username = :username')
            ->setParameter('username', $params['username']);
    }

    //TODO: other params

    return $query->getQuery()->getResult();
}