【发布时间】:2015-12-03 16:02:41
【问题描述】:
所以我在 symfony 中有这个简单的表单,我只有 3 个字段。在这里工作的所有 id 一个不在表单上但在数据库中的实体是 df_date 列,它在数据库中输入 DATE。
这是那个实体:
/**
* @ORM\Column(type="date")
* @ORM\Id
*/
protected $df_date;
/**
* Set df_date
*
* @param \DateTime $dfDate
* @return WeatherSpecials
*/
public function setDfDate($dfDate)
{
$this->df_date = $dfDate;
return $this;
}
/**
* Get df_date
*
* @return \DateTime
*/
public function getDfDate()
{
return $this->df_date;
}
控制器:
public function ajax_weather_specialsAction(Request $request, $main_id)
{
$params = array();
if (!$main_id) {
/*
$repository = $this->getDoctrine()
->getRepository('AppBundle:WeatherSpecials');
$weather_specials = $repository->find($main_id);
*/
} else {
$weather_specials = new WeatherSpecials;
}
$form = $this->createFormBuilder($weather_specials)
->add('df_weather', 'choice', array(
'choices' => array(
null, 'SU', 'PC', 'CL', 'RN'
),
'label' => false, 'required' => true,
))
->add('df_temptur', 'number', array('label' => false, 'required' => true))
->add('df_specday', 'text', array('label' => false, 'required' => false))
->add('save', 'submit', array('attr' => array('class' => 'btn btn-primary')))
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($weather_specials);
// How to set df_date here???
$em->flush();
}
}
$data['status'] = 'success';
$data['html'] = $this->render('sales_tabs/weather_specials.twig', array('form' => $form->createView()))->getContent();
return new JsonResponse($data);
}
问题是,在我将该表单保存到数据库之前如何设置 df_date?
【问题讨论】:
-
你想坚持什么价值?当前时间?
-
任何日期,yyyy-mm-dd,不需要时间。
-
该字段被定义为主键,所以如果你想防止重复键,我建议你存储一个时间戳,最好是一个简单的自动增量?在此处查看文档doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/…
-
日期没问题,因为逻辑确保它每天运行一次。所以它不会重复。
-
抱歉,我认为您已经尝试过使用
$weather_specials->setDfDate(new \DateTime());,但没有成功。也许是在此处doctrine-orm.readthedocs.org/projects/doctrine-dbal/en/latest/… 的教义2 文档中定义的正确 php 类型