1

I am developing web application using symfony framework.I want to fetch records from database of all months.I dont know how to write query for fetching records from jan to december. this is my code:

$totalSearchesByAdminMonth = $em->createQueryBuilder()
    ->select('count(SearchHistory.id) AS totalSearchesByAdmin')
    ->from('DRPAdminBundle:Log',  'SearchHistory')

    ->where('SearchHistory.last_updated like :last_updated')
    ->setParameter('last_updated',$months.'-%')   

    ->andwhere('SearchHistory.event = :event')
    ->setParameter('event','ADMIN_SEARCH')   

        ->getQuery()
        ->getArrayResult();

Please help.

  • take a look at this http://stackoverflow.com/questions/11553183/select-entries-between-dates-in-doctrine-2 – Matteo Jul 30 '15 at 10:03

1 Answers1

1

Assuming SearchHistory.last_updated is a datetime field and $month is the number of months back from now you want to search.

$start = new \DateTime();
$start->sub(new \DateInterval("P{$months}M");

$totalSearchesByAdminMonth = $em->createQueryBuilder()
->select('count(SearchHistory.id) AS totalSearchesByAdmin')
->from('DRPAdminBundle:Log',  'SearchHistory')

->where('SearchHistory.last_updated between :last_updated_start and :last_updated_end')
->setParameter('last_updated_start',$start)   
->setParameter('last_updated_end',new \DateTime())   

->andwhere('SearchHistory.event = :event')
->setParameter('event','ADMIN_SEARCH')   

    ->getQuery()
    ->getArrayResult();
Jason Hendry
  • 193
  • 8