【发布时间】: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