简体   繁体   English

Symfony2 Twig显示上个月的记录

[英]Symfony2 Twig show records for the last month

Following my previous question Ive got a new problem. 在我之前的问题之后,我遇到了一个新问题。 I have 2 entities - authors and books, 1 author may have many books, but 1 book has only 1 author. 我有2个实体-作者和书籍,其中1个作者可能有很多书籍,但是1本书只有1个作者。 Now the book also has date of creation. 现在这本书也有创建日期。 I can show how many books each author has like this, 我可以显示每个作者有多少本书,

Author    Books 
Author1     5
Author2     7 etc...

but along with this in the same table I need to show separately how many books of every author were added in the last month, like this: 但与此同时,在同一表格中,我需要分别显示上个月增加了每位作者的书籍数量,如下所示:

Author    Books   Added in last month
Author1     5            2
Author2     7            3 etc..

In my DB I get all author entities which is mapped with books and this is getting all books for the author, like this: 在我的数据库中,我得到了与书籍映射的所有作者实体,这就是为该作者获得的所有书籍,如下所示:

<td>{{ author.books|length}}</td>

So I think there should be some kind of Twig datetime filter that would show only books, added in the last month. 因此,我认为应该有某种Twig日期时间过滤器,该过滤器仅显示上个月添加的书籍。 Ive seen this question and some more here on SO but they are mainly related to formatting the date rather than dealing with intervals. 我已经看到了这个问题,还有更多关于SO的问题,但它们主要与格式化日期有关,而不是处理间隔。 Any ideas would be welcome, Thank You. 任何想法都将受到欢迎,谢谢。

EDIT 1 Controller code 编辑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);
}

There isn't anything like this in twig. 树枝上没有这样的东西。 You need to Filter your collection on QueryBuilder/DQL (add needed where ) level or using Doctrine Collection Filtering . 您需要在QueryBuilder/DQL (需要where添加)级别上过滤集合,或使用Doctrine 集合过滤来过滤集合

example: 例:

use Doctrine\Common\Collections\Criteria;

// get $author

$bookCollection = $author->getBooks();

$criteria = Criteria::create()
    ->where(Criteria::expr()->gte("added_date", "2015-02-17"))
    ->orderBy(array("added_date" => Criteria::ASC))
    ->setFirstResult(0)
    ->setMaxResults(20)
;

$filteredAuthorBooks = $bookCollection->matching($criteria);

Then pass this to your view. 然后将其传递给您的视图。 Try to do not build any complex logic or filtering in your templates. 尽量不要在模板中建立任何复杂的逻辑或过滤。 Best in your case would be fetching from DB only needed results with join in Author query. 最好在你的情况是从数据库读取只需要结果与joinAuthor查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM