我有一个可以很好地使用SQL的选择,但是我无法使用createQueryBuilder()
将此查询转换为DQL。
这是我的查询:
SELECT distinct c.name_id
FROM cultures AS c
LEFT JOIN ilots AS i ON i.exploitation_id = 1
我当前的代码:
return $this->createQueryBuilder('c')
->leftJoin(Ilots::class, 'i', 'ON', 'i.exploitation = 1')
->distinct()
->getQuery()
->getResult();
和错误:
[[语法错误]行0,列74:错误:预期的字符串结尾,'ON'
在DQL中ON
不存在,必须改为使用WITH
。
return $this->createQueryBuilder('c')
->leftJoin(Ilots::class, 'i', 'WITH', 'i.exploitation = 1')
->distinct()
->getQuery()
->getResult();
如果实体之间存在关系:
$qb = $this->entityManager->createQueryBuilder();
$qb
->select('c')
->distinct()
->from('cultures', 'c')
->leftJoin('c.ilots', 'i')
->where('i.exploitation = 1')
;
return $qb->getQuery()->getResult();