我想计算本周的记录。但我不知道我该怎么做。
这是我的问题:
$totalSearchesByUsersWeek = $em->createQueryBuilder()
->select('count(SearchHistory.id) AS totalSearchesByUsers')
->from('DRPAdminBundle:Log', 'SearchHistory')
->where('SearchHistory.last_updated like :last_updated')
->setParameter('last_updated',$week.'%')
->getQuery()
->getArrayResult();
请帮忙
答案 0 :(得分:3)
您可以将COUNT()
与2个getSingleScalarResult
个对象以及您对本周开始和结束的定义结合使用。使用$start = new \DateTime();
// This will depend on which day you decide the week starts
if ('Monday' !== $start->format('l')) {
// if week starts on Sunday you would just use (int) $start->format('w')
$start->modify(sprintf('- %d days', (int) $start->format('w') - 1));
}
// clone the start date and add 6 days to get the end date
$end = clone($start);
$end->modify('+ 6 days');
// set the times to midnight for start and 1 second before midnight for finish
$start->setTime(0, 0, 0);
$end->setTime(23, 59, 59);
// for the date 2015-07-16 (today) this would result in
// start = 2015-07-13 00:00:00 (Monday)
// end = 2015-07-19 23:59:59 (Sunday)
$queryBuilder = $em->createQueryBuilder();
return $queryBuilder
->select('COUNT(o.id)')
->from('DRPAdminBundle:Log', 'o')
->where($queryBuilder->expr()->between('o.last_updated', ':start', ':end'))
->setParameter('start', $start)
->setParameter('end', $end)
->getQuery()
->getSingleScalarResult();
和$queryBuilder = $em->createQueryBuilder();
return $queryBuilder
->select('COUNT(o.id)')
->from('DRPAdminBundle:Log', 'o')
->where($queryBuilder->expr()->gte('o.last_updated', ':start'))
->setParameter('start', new \DateTime('- 7 days'))
->getQuery()
->getSingleScalarResult();
,您只需获得单个整数计数即可。
presentModalViewController
同时强>
如果您只想过去7天就可以使用..
CustomModalViewController *modalVC =[[CustomModalViewController alloc]init];
[self presentModalViewController:modalVC animated:YES];
答案 1 :(得分:0)
将getArrayResult()
替换为getSingleScalarResult()
,您将很高兴
编辑以回答您的评论:
$totalSearchesByUsersWeek = $em->createQueryBuilder()
->select('SearchHistory.id AS totalSearchesByUsers')
->from('DRPAdminBundle:Log', 'SearchHistory')
->where('SearchHistory.last_updated like :last_updated')
->setParameter('last_updated',$week.'%')
->getQuery();
$totalSearchesByUsersWeek->->getArrayResult(); //this will get you the array of id
和
$totalSearchesByUsersWeek = $em->createQueryBuilder()
->select('count(SearchHistory.id) AS totalSearchesByUsers')
->from('DRPAdminBundle:Log', 'SearchHistory')
->where('SearchHistory.last_updated like :last_updated')
->setParameter('last_updated',$week.'%')
->getQuery();
$totalSearchesByUsersWeek->->getSingleScalarResult(); //this will get you the COUNT of id
Edit2:用这个
替换你的where子句->where('SearchHistory.last_updated BETWEEN :monday AND :sunday')
->setParameter('monday', '2014-7-13')
->setParameter('sunday', '2014-7-19'); //or adjust date format accordingly