2017-06-15 91 views
0

我想算我的主題是多少次在我的博客中。DQL請求與LeftJoin

任何博客文章(法語文章)可以使用一個或多個主題。

所以我有一個表(多對多):

  • themes_articles(id_theme,id_article)和:
  • 主題(id_theme,nom_theme)
  • 文章(博客文章)(id_article,說明.. 。)

在SQL中,我做的:

SELECT T.id,nom_theme,count(A.themes_id) from themes_articles A right join themes T on T.id=A.themes_id group by nom_theme 

它的工作原理,但是當我想使用DQL右連接,這是一個有點硬。我切換我的兩個表使用左連接,但我不知道如何在這裏使用我的關係表(themes_articles)。

我想是這樣的:

$query = $this->createQueryBuilder('') 
->select(array('T.id', 'nomTheme', 'count(A.themes_id) as nombre')) 
->from('themes', 'T') 
->leftJoin('themes_articles', 'A', 'WITH', 'T.id= A.themes_id') 
->groupBy('nom_theme'); 
    return $query->getQuery()->getResult(); 

但它不工作。

[Semantical Error] line 0, col 93 near 'themes_articles': Error: Class 'themes_articles' is not defined. 

如何在DQL請求中轉換我的SQL請求?

謝謝你很多的幫助。

回答

0

使用這樣

$query = $this->createQueryBuilder() 
    ->select(array('t.id', 't.nomTheme', 'count(ta.themes_id) as nombre')) 
    ->from('<YOUR BUNDLE>:<Theam Entity Class>', 't') //Like AcmeTheamBundle:Themes 
    ->leftJoin('t.themes_articles') 
    ->groupBy('t.nomTheme'); //better to use theam id 
    return $query->getQuery()->getResult(); 
+0

您好,我想你的要求,但我有這個錯誤 [語法錯誤] 0行,列87:錯誤:字符串應爲結束,得到了「BlogBu​​ndle:主題」 – devicz

+0

凡位於你的實體?請提供此路徑 – Tester

+0

我添加了一個答案更多信息 – devicz

0

這是一個更好一點!我必須在我的請求中添加文章和主題(contient)之間的關係名稱。

$query = $this->createQueryBuilder('t') 
    ->select(array('t.id', 't.nomTheme', 'count(ta.id) as nombre')) 
    ->leftJoin('t.contient', 'ta', 'WITH', 't.id= ta.id') 
    ->groupBy('t.nomTheme'); //better to use theam id 
    return $query->getQuery()->getResult();