【问题标题】:Get records of all months in symfony [duplicate]在 symfony 中获取所有月份的记录 [重复]
【发布时间】:2015-10-21 14:54:34
【问题描述】:

我正在使用 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();

请帮忙。

【问题讨论】:

标签: php symfony


【解决方案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();

【讨论】:

  • 我在这个 $start->sub(new \DateInterval("P{$months}M");
  • 未定义变量:months.how 从现在开始返回几个月
猜你喜欢
  • 2017-08-22
  • 1970-01-01
  • 1970-01-01
  • 2014-02-12
  • 2023-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多