简体   繁体   English

Doctrine 查询 SQL 到 DQL

[英]Doctrine query SQL to DQL

I have a select with SQL which is working perfectly, but I cannot translate this query to DQL with createQueryBuilder() .我有一个 SQL 选择,它运行良好,但我无法使用createQueryBuilder()将此查询转换为 DQL。

Here's my query:这是我的查询:

SELECT distinct c.name_id
FROM cultures AS c
LEFT JOIN ilots AS i ON i.exploitation_id = 1

My current code:我目前的代码:

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

And the error:和错误:

[Syntax Error] line 0, col 74: Error: Expected end of string, got 'ON' [语法错误] 第 0 行,第 74 列:错误:预期的字符串结尾,得到 'ON'

In DQL ON doesn't exist, you have to use WITH instead.在 DQL ON中不存在,您必须改用WITH

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

Documentation 文档

If there is relationship between the Entity:如果实体之间存在关系:

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

    return $qb->getQuery()->getResult();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM