我正在使用symfony框架开发Web应用程序。我想从所有月份的数据库中获取记录。我不知道如何编写查询以获取从1月到12月的记录。 这是我的代码:
$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();
请帮忙。
答案 0 :(得分:1)
假设SearchHistory.last_updated是一个日期时间字段,$ month是从现在开始想要搜索的月数。
$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();