【发布时间】:2016-03-28 08:49:13
【问题描述】:
在我的previous 问题之后,我遇到了一个新问题。我有 2 个实体 - 作者和书籍,1 个作者可能有很多书,但 1 本书只有 1 个作者。现在这本书也有创作日期。我可以显示每个作者有多少这样的书,
Author Books
Author1 5
Author2 7 etc...
但除此之外,我还需要在同一张表中分别显示每个作者在上个月添加了多少本书,如下所示:
Author Books Added in last month
Author1 5 2
Author2 7 3 etc..
在我的数据库中,我得到所有与书籍映射的作者实体,这是为作者获取所有书籍,如下所示:
<td>{{ author.books|length}}</td>
所以我认为应该有某种 Twig 日期时间过滤器,只显示书籍,在上个月添加。我在 SO 上看到了this 问题和更多问题,但它们主要与格式化日期有关,而不是处理间隔。欢迎任何想法,谢谢。
编辑 1 个控制器代码
<?php
namespace AB\ProjectBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Doctrine\Common\Collections\Criteria;
use Symfony\Component\HttpFoundation\Response;
use AB\ProjectBundle\Entity\Book;
class AuthorController extends Controller
{
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$userid = $this->container->get('security.context')->getToken()->getUser()->getId();
$entity = $em->getRepository('ABProjectBundle:Authors')->findBy(array('userid'=>$userid));
$books = $entity[0]->getBooks();
$criteria = Criteria::create()
->where(Criteria::expr()->gte("datecreation", "2016-03-15"))
->orderBy(array("datecreation" => Criteria::ASC))
->setFirstResult(0)
->setMaxResults(20)
;
$filtered = $books->matching($criteria);
$twig = 'ABProjectBundle::index.html.twig';
$paramTwig = array(
'authors' => $entity,
'filtered' => $filtered,
);
return $this->render($twig, $paramTwig);
}
【问题讨论】: