我对 Symfony 2.1 和将日期持久化到数据库有疑问。我使用 PHP 5.3。我的代码如下:
public function moveAction()
{
$request = $this->getRequest();
$id = $request->get('id');
$daydelta = $request->get('daydelta');
$minutedelta = $request->get('minutedelta');
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('MedSystemTimetableBundle:Appointment')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Appointment entity.');
}
if (0 != $daydelta) {
$day = ($daydelta > 0) ? '+'.$daydelta : $daydelta;
$day .= (abs($daydelta) == 1) ? ' day' : ' days';
$modifyFrom = $entity->getTimeFrom()->modify($day);
$modifyTo = $entity->getTimeTo()->modify($day);
$entity->setTimeFrom($modifyFrom);
$entity->setTimeTo($modifyTo);
}
if (0 != $minutedelta) {
$month = ($minutedelta > 0) ? '+'.$minutedelta : $minutedelta;
$month .= (abs($minutedelta) == 1) ? ' minute' : ' minutes';
$modifyFrom = $entity->getTimeFrom()->modify($month);
$modifyTo = $entity->getTimeTo()->modify($month);
$entity->setTimeFrom($modifyFrom);
$entity->setTimeTo($test);
}
$em->flush();
return new Response('ok', 200);
}
我已经转储了新的时间值并且它正确更改了,但由于某种原因它没有保存到数据库中。但是,如果我将值设置为 new \DateTime 它就可以了。对于这种奇怪的行为有什么想法吗?