【问题标题】:Doctrine2 - How to setup a query by months?Doctrine2 - 如何按月设置查询?
【发布时间】:2014-03-30 23:03:10
【问题描述】:

一些快速的上下文我正在边栏中创建一个基于月/年的博客存档。我现在需要显示那个月的帖子。我需要一些帮助来创建此查询以传递给树枝。

以下是原始问题的链接以及我迄今为止的设置,查询显然不正确(关于这篇文章的性质)并且可以使用一些帮助来充实这一点。

如果向我展示如何在一个月内为帖子创建正确的查询并将其链接到树枝来完成这些最后步骤,我们将不胜感激。

Symfony2 - Setting up a blog archive

到目前为止,这就是我所拥有的。

查询(这部分需要帮助 - 如何设置查询以按月/年检索帖子)

public function getPostsByMonth($year, $month)
{
    $qb = $this->createQueryBuilder('b')
    ->from('Blog', 'b')
        ->select('b')
    ->where('created BETWEEN :june AND :july')
        ->setParameter('june', $june->format('2013-June'))
        ->setParameter('july', $july->format('2013-July'))
        ???
}

路线

general_sym_project_archive:
 path:  /archive/{year}/{month}
 defaults: { _controller: GeneralSymProjectBundle:Blog:archive }

控制器

public function archiveAction($year, $month)
{
    $em = $this->getDoctrine()->getManager();

    $blogs = $em->getRepository('GeneralSymProjectBundle:Blog')
        ->getPostsByMonth($year, $month);

    if (!$blogs) {
        throw $this->createNotFoundException('Unable to find blog posts');
    }

    foreach ($blogs as $post) {
        $year = $post->getCreated()->format('Y');
        $month = $post->getCreated()->format('F');
        $blogPosts[$year][$month][] = $post;
    }



    return $this->render('GeneralSymProjectBundle:Default:archive.html.twig', array(
        'blogPosts' => $blogPosts,
    ));

树枝(需要按月将帖子链接到此树枝)

<h4>Archives</h4>
                    <ol class="list-unstyled">
                        <li><a href="{{ path('general_sym_project_archive', { 'year': '2013', 'month': '07'}) }}">July 2013</a></li>
                    </ol>

【问题讨论】:

  • $june$july 定义在哪里?
  • 它没有定义它只是我正在试验的......有什么帮助吗?
  • 你的问题是什么?阻碍你的错误是什么?
  • 问题在于不知道如何设置查询。

标签: symfony datetime doctrine-orm twig


【解决方案1】:

假设您的 created 字段是日期时间,并且您想获取给定年份中给定月份的帖子,这应该可行:

public function getPostsByMonth($year, $month)
{
    $date = new \DateTime("{$year}-{$month}-01");

    $qb = $this->createQueryBuilder('b');
    $query = $qb
        ->where('b.created BETWEEN :start AND :end')
        ->setParameter('start', $date->format('Y-m-d'))
        ->setParameter('end', $date->format('Y-m-t'))
    ;
    return $query->getQuery()->getResult();
}

【讨论】:

  • $date 变量上的 -01 是什么意思?我是否需要更改路线以包含此路线和控制器?按原样使用我得到以下信息:(ContextErrorException:警告:缺少 General\SymProjectBundle\Entity\Repository\BlogRepository::getPostsByMonth() 的参数 1,在 /var/www/html/SymProject/src/General/SymProjectBundle/ 中调用Controller/BlogController.php 位于第 127 行,定义在 /var/www/html/SymProject/src/General/SymProjectBundle/Entity/Repository/BlogRepository.php 第 29 行)
  • '-01' 将 DateTime 对象设置为该月的第一天。您的错误表明您没有将参数传递给对getPostsByMonth() 的调用,即$year 和$month。
  • 嗯,我已经更新了控制器以使用 getPostsByMonth() 而不是 getLatestBlogs()。哪个加载页面很好,但是当我点击一个分配了年份和月份的月份时,它会抛出错误? (
  • 2013 年 7 月
  • 我需要在控制器内部将 $year 和 $month 分配为 null 吗?
  • 啊,您必须将它们的格式转换为“m”。如果您希望网址中包含“F”,则必须在调用该函数之前在控制器中执行类似的操作:` $date = new \DateTime("{$month} 01, {$year} "); $month = $date->format('m');`
  • 猜你喜欢
    相关资源
    最近更新 更多
    热门标签