2017-06-15 54 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();