【问题标题】:Symfony Doctrine QueryBuilder - Get the latest published news of each categorySymfony Doctrine QueryBuilder - 获取每个类别的最新发布新闻
【发布时间】:2019-08-19 10:12:34
【问题描述】:

所以我已经看到很多关于我的问题的主题(每组最大的数),但似乎我无法让它与 queryBuilder 一起使用! (例如:post
我正在运行 symfony 4.2.2

实体

-------------                    -------------
News                             Taxonomy
-------------                    -------------
id                               id
title                            name
category (ManyToOne Taxonomy)    value
status (ManyToOne Taxonomy)
publication_date

Category 可以采用以下值:tech / sports / games .. 等等。
Status 可以采用以下值:draft / schedule / published .

我想要什么

在我使用 queryBuilder 的 NewsRepository 中,我想返回 每个类别最新 发布新闻。

我最终做了什么

return $this->createQueryBuilder('a')
            ->select('n news', 'n.publicationDate pubDate')
            ->leftJoin('n.category', 'c')
            ->addSelect('c.value category')
            ->leftJoin('n.status', 's')
            ->addSelect('s.value status')
            ->andWhere('s.value = :st')
            ->setParameter('st', Taxonomy::WORKFLOW_PUBLISHED)
            ->orderBy('n.category, n.datePublication', 'DESC')
            ->getQuery()
            ->getResult();

Taxonomy::WORKFLOW_PUBLISHED 是一个类常量存储:'published' 然后在服务中,我只是迭代前一个方法返回的结果并提取每个类别的最新新闻。
我不喜欢这个临时解决方案,所以如果有人可以帮助我使用 queryBuilder,那就太好了!

+ 附加信息

我尝试在类别上使用 groupby,但它没有返回最新值。
如果可能的话,我也在寻找好的性能。

【问题讨论】:

    标签: symfony doctrine greatest-n-per-group query-builder


    【解决方案1】:

    如果您看到原始的 post: Doctrine Query Language get Max/Latest Row Per Group,您将需要为您的 News 实体进行自联接,以便组织您想要的数据(每个类别的最新新闻),在调整原始帖子后,您的查询构建器可能看起来像

    return $this->createQueryBuilder('a')
                ->select('n news', 'n.publicationDate pubDate')
                ->leftJoin(
                    'AppBundle\Entity\News',
                    'n1',
                    'WITH',
                    'n.category = n1.category AND n.datePublication < n1.datePublication'
                )
                ->leftJoin('n.category', 'c')
                ->addSelect('c.value category')
                ->leftJoin('n.status', 's')
                ->addSelect('s.value status')
                ->where( 'n1.datePublication IS NULL' )
                ->andWhere('s.value = :st')
                ->setParameter('st', Taxonomy::WORKFLOW_PUBLISHED)
                ->orderBy('n.category, n.datePublication', 'DESC')
                ->getQuery()
                ->getResult();
    

    【讨论】:

      【解决方案2】:

      您是否在查询或表单中尝试过 groupBy?

      也许表单中的“group_by”选项可以完成这项工作:

      'query_builder' => function(NewsRepository $r)use($options)
                      {
                          return $r->yourQuery($options['opt']);
                      },
      'group_by' => function(News $news)
                      {
                          return $news->getCategory()->getName();
                      },
      

      我的意思是,如果您在查询中使用 orderBy 来获取最新消息,并在表单中按类别名称使用“group_by”,它将起作用。

      通过query_builder获取订单,最后用表单中的group_by选项排序。

      【讨论】:

      • 嗨,我在查询中尝试了 groupBy。就我而言,没有涉及任何形式。
      【解决方案3】:

      因为 group by 不关心数据的顺序,所以你不能通过一个查询来做到这一点。

      最好使用group by category并选择max(n.id) 然后通过这些 id 进行另一个查询以查找相关新闻

      【讨论】:

      • 嗨,我知道这种可能性,但我想要一个使用查询构建器的解决方案,因为这是我苦苦挣扎的原因。
      猜你喜欢
      • 2020-10-06
      • 1970-01-01
      • 2014-07-19
      • 2021-01-31
      • 1970-01-01
      • 2016-12-25
      • 2018-02-16
      • 2016-08-18
      • 2020-12-02
      相关资源
      最近更新 更多