use*_*420 13 php datetime doctrine symfony doctrine-orm
我有一个Syfmony2应用程序,其中包含一个具有日期字段的表.此日期字段是DateTime类型.
我需要获得与现在相同的所有实体.
但如果我这样做:
$now = new \DateTime();
$data = $entityRepository->findByDate($now);
Run Code Online (Sandbox Code Playgroud)
我得到0结果,因为Doctrine正在比较DateTime对象,我需要只比较年,月和日,而不是小时...只有de Date对象,而不是DateTime.
任何的想法?感谢:D
got*_*oto 40
我看到这个简单的方法:
$now = new \DateTime();
$data = $entityRepository->getByDate($now);
Run Code Online (Sandbox Code Playgroud)
然后在你的存储库中
public function getByDate(\Datetime $date)
{
$from = new \DateTime($date->format("Y-m-d")." 00:00:00");
$to = new \DateTime($date->format("Y-m-d")." 23:59:59");
$qb = $this->createQueryBuilder("e");
$qb
->andWhere('e.date BETWEEN :from AND :to')
->setParameter('from', $from )
->setParameter('to', $to)
;
$result = $qb->getQuery()->getResult();
return $result;
}
Run Code Online (Sandbox Code Playgroud)
小智 5
存储库中的方法
public function getDays(\DateTime $firstDateTime, \DateTime $lastDateTime)
{
$qb = $this->getEntityManager()->createQueryBuilder()
->select('c')
->from('ProjectBundle:Calendar', 'c')
->where('c.date BETWEEN :firstDate AND :lastDate')
->setParameter('firstDate', $firstDateTime)
->setParameter('lastDate', $lastDateTime)
;
$result = $qb->getQuery()->getResult();
return $result;
}
Run Code Online (Sandbox Code Playgroud)
和行动
public function calendarAction()
{
$currentMonthDateTime = new \DateTime();
$firstDateTime = $currentMonthDateTime->modify('first day of this month');
$currentMonthDateTime = new \DateTime();
$lastDateTime = $currentMonthDateTime->modify('last day of this month');
$days = $this->getDoctrine()
->getRepository('ProjectBundle:Calendar')
->getDays($firstDateTime, $lastDateTime);
return ['days' => $days];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
32497 次 |
| 最近记录: |