我的表中有日期字段,但它们包含所有内容 - 日,年和月。我是否可以编写查询以仅获取具有月份等于当月的记录?我可以这样做:
$today = new \DateTime();
$month = $today->format('m');
$cat = $em->getRepository('EMBudgetTrackerBundle:Expense')->find(1);
$ex_date = $cat->getDate();
并比较$ month和$ ex_date,但是我可以编写某种查询吗?像这样:
public function getExpensesByMonth($month)
{
$q = $this->createQueryBuilder('e');
$q->select('e')
->where('e.date = :date')
->setParameter('date', $month);
return $q->getQuery()->getResult();
}
提前谢谢! :)
答案 0 :(得分:2)
如果数据库列是DateTime格式,则可以在查询中使用DateTime对象。据我所知,你只能查询时间范围。
public function getExpensesByMonth($beginning, $end)
{
$q = $this->createQueryBuilder('e');
$q->select('e')
->where('e.date > :beginning')
->andWhere('e.date < :end')
->setParameter('beginning', $beginning)
->setParameter('end', $end);
return $q->getQuery()->getResult();
}