2016-01-28 64 views
2

在一個使用Doctrine 2和postgreSQL數據庫的項目中,實體婦科醫生和他的電子郵件和電話號碼之間有一個簡單的一對多關係。我想查詢數據庫以獲得第一個插入的電子郵件和號碼的每個人的一行。 我期望的結果如下所示:如何獲得與Doctrine和PostgreSQL的左連接的第一個結果

------------------- 
SURNAME | NAME | EMAIL   | TEL. NR.| 
Surname1 | Name1 | [email protected] | number1 | 
------------------- 

我嘗試:

$columns = array('g.id', 'g.companyName', 'g.surname', 'g.name', 'e.email', 't.number'); 

$queryBuilder = $this->getDoctrine() 
    ->getRepository('AppBundle:Gynecologist') 
    ->createQueryBuilder('g') 
    ->select($columns) 
    ->join('g.emails', 'e') 
    ->join('g.telephoneNumbers', 't') 
    ->orderBy('g.surname', 'ASC') 
    ->addOrderBy('g.name', 'ASC') 
    ->groupBy('g.id') 
    ->addGroupBy('g.companyName') 
    ->addGroupBy('g.surname') 
    ->addGroupBy('g.name') 
    ->addGroupBy('e.email') 
    ->addGroupBy('t.number'); 

這是產生的查詢:

SELECT g0_.id AS id_0, g0_.company_name AS company_name_1, g0_.surname AS surname_2, g0_.name AS name_3, g1_.email AS email_4, g2_.number AS number_5 
FROM gynecologists 
g0_ INNER JOIN gynecologists_emails g1_ ON g0_.id = g1_.gynecologist_id 
INNER JOIN gynecologists_telephone_numbers g2_ ON g0_.id = g2_.gynecologist_id 
GROUP BY g0_.id, g0_.company_name, g0_.surname, g0_.name, g1_.email, g2_.number 
ORDER BY g0_.surname ASC, g0_.name ASC 
LIMIT 500 OFFSET 0 

根據由StackOverflow的用戶接收一個建議,我也嘗試過:

$columns = array('DISTINCT g.id', 'g.companyName', 'g.surname', 'g.name', 'e.email', 't.number'); 

$queryBuilder = $this->getDoctrine() 
    ->getRepository('AppBundle:Gynecologist') 
    ->createQueryBuilder('g') 
    ->select($columns) 
    ->join('g.emails', 'e') 
    ->join('g.telephoneNumbers', 't') 
    ->orderBy('g.surname', 'ASC') 
    ->addOrderBy('g.name', 'ASC'); 

SELECT DISTINCT g0_.id AS id_0, g0_.company_name AS company_name_1, g0_.surname AS surname_2, g0_.name AS name_3, g1_.email AS email_4, g2_.number AS number_5 
FROM gynecologists g0_ 
INNER JOIN gynecologists_emails g1_ ON g0_.id = g1_.gynecologist_id 
INNER JOIN gynecologists_telephone_numbers g2_ ON g0_.id = g2_.gynecologist_id 
ORDER BY g0_.surname ASC, g0_.name ASC 
LIMIT 500 OFFSET 0 

但結果總是:

------------------- 
SURNAME | NAME | EMAIL   | TEL. NR.| 
Surname1 | Name1 | [email protected] | number1 | 
Surname1 | Name1 | [email protected] | number2 | 
Surname1 | Name1 | [email protected] | number1 | 
Surname1 | Name1 | [email protected] | number2 | 
------------------- 

謝謝。

+0

當使用GROUP BY,選擇所有列必須是GROUP BY列,或與聚合函數中使用,如MAX(),AVG() ... –

+0

好的。但是,如何解決查詢只返回每行一行?按選定的分組進行分組,每個人得到的行數與保存的電子郵件數量或電話號碼一致... – Gianluca78

+0

您可以添加生成的查詢嗎? –

回答

0

我認爲它應該爲你工作:

$columns = array('DISTINCT g.id', 'g.companyName', 'g.surname', 'g.name', 'e.email', 't.number'); 

$queryBuilder = $this->getDoctrine() 
    ->getRepository('AppBundle:Gynecologist') 
    ->createQueryBuilder('g') 
    ->select($columns) 
    ->join('g.emails', 'e') 
    ->join('g.telephoneNumbers', 't') 
    ->orderBy('g.surname', 'ASC') 
    ->addOrderBy('g.name', 'ASC'); 
+0

它不起作用。我根據你的建議編輯了我的消息。謝謝 – Gianluca78

相關問題