-1

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. 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. Ive seen this question and some more here on SO but they are mainly related to formatting the date rather than dealing with intervals. Any ideas would be welcome, Thank You.

EDIT 1 Controller code

<?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);
}
Community
  • 1
  • 1
Jack
  • 857
  • 14
  • 39

1 Answers1

2

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.

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.

Paweł Mikołajczuk
  • 3,692
  • 25
  • 32
  • Im passing it to the view with |length, but get only zeroes in the view. If I remove |length, I get Doctrine\Common\Collections\ArrayCollection@0000000023bf3ae5000000006ee44d9f for every author. Any ideas what this can be? – Jack Mar 28 '16 at 09:53
  • Now it is saying FatalErrorException in DateTimeType.php line 53: Error: Call to a member function format() on string. The type of my added_date column in DB is Timestamp, changed it to DateTime, but it did not help. – Jack Mar 28 '16 at 10:03
  • Changed the type of the column in orm.yml file to string and it solved the issue, but I dont know whether it is the right way to do that. Now it is getting the number of books created by the first author in the last month correctly and it shows this same number for every remaining author in the table(( – Jack Mar 28 '16 at 10:21
  • It should work with date_time. I can't help without seeing your entities and controller code. – Paweł Mikołajczuk Mar 28 '16 at 10:46
  • entities remained the same as in previous question except new property datecreation added. Add controller code in a minute – Jack Mar 28 '16 at 13:41