当我使用 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行,col 73:错误:预期字面,获得'join'
请参阅此答案:https://stackoverflow.com/questions/17989473/symfony2-doctrine-multiple-joins-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();
}