【问题标题】:Symfony3 querybuilder orderby count few manytomanySymfony3 查询构建器 orderby 计数很少很多
【发布时间】:2017-02-25 21:45:29
【问题描述】:

我有一个名为 Tag 的实体,它有 3 个 ManyToMany 关系节、文章和新闻。

AppBundle\Entity\Tag:
    type: entity
    table: tag
    ...
  manyToMany:
      news:
          orderBy: { 'posted': 'DESC' }
          targetEntity: News
          inversedBy: tags
          joinTable:
            name: news_tag
            joinColumns:
              tag_id:
                referencedColumnName: id
            inverseJoinColumns:
              news_id:
                referencedColumnName: id
      articles:
          orderBy: { 'posted': 'DESC' }
          targetEntity: Article
          inversedBy: tags
          joinTable:
            name: article_tag
            joinColumns:
              tag_id:
                referencedColumnName: id
            inverseJoinColumns:
              article_id:
                referencedColumnName: id
      fests:
          orderBy: { 'when_starts': 'DESC', 'when_ends': 'DESC' }
          targetEntity: Fest
          inversedBy: tags
          joinTable:
            name: fest_tag
            joinColumns:
              tag_id:
                referencedColumnName: id
            inverseJoinColumns:
              fest_id:
                referencedColumnName: id

现在我想编写一个 createQueryBuilder,它按计数“新闻”+ 计数“文章”+ 计数“节日”排序,仅用于查找“TOP TAGS”。我只为一个多对多关系找到了解决方案。

有什么办法吗?

编辑: 我有什么:

    $qb->select(array(
        't.id', 
        't.name', 
        'COUNT(f) as festcount',
        'COUNT(n) as newscount',
        'COUNT(a) as articlescount',
        '(COUNT(f) + COUNT(n) + COUNT(a)) as totalcount'
        ))
    ->from('AppBundle:Tag', 't')
    ->leftJoin('t.fests', 'f')
    ->leftJoin('t.articles', 'a')
    ->leftJoin('t.news', 'n')
    ->groupBy('t.id')
    ->orderBy('totalcount', 'DESC');

但它给出了虚假的结果。当某个标签有 3 个 fests、1 个文章和 0 个新闻时,结果是 festcount = 3,newscount = 0,articlescount = 3,totalcount = 6,应该是 4。

【问题讨论】:

  • Post 缺少您想要实现的代码。
  • @michail_w 感谢您的评论,我写道我需要“构建”createQueryBuilder,它将选择按所有关系(新闻+文章+节日)DESC 的计数总和排序的标签。
  • StackOverflow 的规则是帮助人们解决问题,而不是完成他们的工作。在您发布您已经拥有的 queryBuilder 代码之前,没有人会回答这个问题。
  • @michail_wi 我更新了我的问题。

标签: php sql doctrine symfony


【解决方案1】:

试试这个代码:

$qb->select(array(
        't.id', 
        't.name', 
        'COUNT(DISTINCT f.id) as festcount',
        'COUNT(DISTINCT n.id) as newscount',
        'COUNT(DISTINCT a.id) as articlescount',
        '(festcount + newscount + articlescount) as totalcount'
        ))
    ->from('AppBundle:Tag', 't')
    ->leftJoin('t.fests', 'f')
    ->leftJoin('t.articles', 'a')
    ->leftJoin('t.news', 'n')
    ->groupBy('t.id')
    ->orderBy('totalcount', 'DESC');

如果您需要进一步的帮助,请提供这些表的转储。

【讨论】:

  • 哇!我不知道为什么,但它有效。顺便提一句。这一行:'(festcount + newscount + articlescount) as totalcount' 我需要写成:(COUNT(DISTINCT f) + COUNT(DISTINCT n) + COUNT(DISTINCT a)) as totalcount 因为我遇到错误SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sclr_2' in 'field list'
猜你喜欢
  • 1970-01-01
  • 2022-12-11
  • 2014-09-15
  • 1970-01-01
  • 2015-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-26
相关资源
最近更新 更多