簡體   English   中英

Doctrine 查詢 SQL 到 DQL

[英]Doctrine query SQL to DQL

我有一個 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('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