SQL查询SQL到DQL

问题描述 投票:1回答:2

我有一个可以很好地使用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'

php mysql doctrine
2个回答
1
投票

在DQL中ON不存在,必须改为使用WITH

return $this->createQueryBuilder('c')
        ->leftJoin(Ilots::class, 'i', 'WITH', 'i.exploitation = 1')
        ->distinct()
        ->getQuery()
        ->getResult();

Documentation


-1
投票

如果实体之间存在关系:

$qb = $this->entityManager->createQueryBuilder();
    $qb
        ->select('c')
        ->distinct()
        ->from('cultures', 'c')
        ->leftJoin('c.ilots', 'i')
        ->where('i.exploitation = 1')
        ;

    return $qb->getQuery()->getResult();
© www.soinside.com 2019 - 2024. All rights reserved.