【问题标题】:Symfony 2 + Doctrine | Entity must have all tagsSymfony 2 + 教义 |实体必须具有所有标签
【发布时间】:2014-06-19 14:17:03
【问题描述】:

我正在使用 Symfony 2 和学说。我有 4 个实体,CustomPage、Tag、CustomPageTag、Comment。

每个 CustomPage 可以有许多标签和许多 cmets。 我想选择一个带有特定标签的 customPage(包含所有 cmets)。

示例:我想选择带有 'tag1' AND 'tag2' 的 customPage

我这样做了,

SELECT CUSTOMPAGE, TAG, CUSTOMPAGE_TAG, COMMENT
FROM CustomBundle:CustomPage CUSTOMPAGE
JOIN CUSTOMPAGE.comments COMMENT
JOIN CUSTOMPAGE.tags CUSTOMPAGE_TAG
JOIN CUSTOMPAGE_TAG.tag TAG
WHERE TAG.name IN ('tag1','tag2')
GROUP BY CUSTOMPAGE.id
HAVING COUNT(DISTINCT CUSTOMPAGE_TAG.name) = 2

效果很好,但问题是它只返回一条评论(我想要全部)。

【问题讨论】:

  • 你尝试左加入三个表而不是加入?
  • 不,一样!它返回了良好的结果,但没有评论!

标签: mysql sql symfony doctrine


【解决方案1】:

由于使用 group by,您只会得到一条评论,而不是使用两条 join。

试试这个:

SELECT CUSTOMPAGE, TAG1, CUSTOMPAGE_TAG, COMMENT
FROM CustomBundle:CustomPage CUSTOMPAGE
JOIN CUSTOMPAGE.comments COMMENT
JOIN CUSTOMPAGE.tags CUSTOMPAGE_TAG
JOIN CUSTOMPAGE_TAG.tag TAG1
JOIN CUSTOMPAGE_TAG.tag TAG2
WHERE TAG1.name= 'tag1'
AND TAG2.name= 'tag2'

【讨论】:

    【解决方案2】:

    我终于找到了!原则允许使用存在!所以:

    $qb->expr()->exists('SELECT b...') 
    

    谢谢大家的帮助。

    【讨论】:

      猜你喜欢
      • 2017-12-04
      • 2014-02-06
      • 1970-01-01
      • 1970-01-01
      • 2012-01-17
      • 2018-08-31
      • 1970-01-01
      • 1970-01-01
      • 2018-04-28
      相关资源
      最近更新 更多