php 在 symfony2 形式的日期时间字段上设置默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8713200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set default value on Datetime field in symfony2 form
提问by i.am.michiel
I have a form containing several fields. One of them is a Datetime field. How to define a default value for that field?
我有一个包含多个字段的表单。其中之一是日期时间字段。如何为该字段定义默认值?
I've tried setting a value on the related entity, in controller, in constructor and __construct :
我已经尝试在相关实体、控制器、构造函数和 __construct 中设置一个值:
$myEntity = new MyEntity();
$myEntity->setMyDate(new \DateTime());
$form = $this->createForm(new AddMyEntity(), $myEntity);
Not working.
不工作。
Tried to define the $data
variable in the buildForm :
试图$data
在 buildForm 中定义变量:
$builder->add('myDate', 'date', array(
'format' => \IntlDateFormatter::SHORT,
'input' => 'datetime',
'widget' => 'single_text',
'data' => new \DateTime("now"));
Not working either. Any ideas, Symfony2 community?
也不工作。任何想法,Symfony2 社区?
EDIT : Adding entity on demand of faost.
编辑:根据 faost 的需求添加实体。
/**
* @ORM\Column(name="myDate", type="datetime")
* @Assert\NotBlank()
*/
private $myDate;
回答by Elnur Abdurrakhimov
Set it in the entity constructor:
在实体构造函数中设置它:
class Entity
{
/**
* @var \DateTime
*/
private $date;
public function __construct()
{
$this->date = new \DateTime();
}
}
回答by Adam Elsodaney
Elnur's answer is correct and is perhaps the recommended one. But for completeness, an alternative way to set the default value for a date widget in a form is to specify the data
key in the options array argument with an instance of DateTime.
Elnur 的答案是正确的,也许是推荐的答案。但为了完整起见,另一种为表单中的日期小部件设置默认值的方法是使用data
DateTime 实例指定options 数组参数中的键。
$builder->add('myDate', 'date', array(
'data' => new \DateTime()
));
Note: This will overwrite the previously set datetime on every edit.
注意:这将在每次编辑时覆盖先前设置的日期时间。
回答by Henry
This solution doesn't require modifying your entity object.
此解决方案不需要修改您的实体对象。
$builder->add('myDate', DateTimeType::class, [
'label' => 'My Date',
'required' => false,
'date_widget' => 'single_text',
'time_widget' => 'single_text',
'date_format' => 'dd/MM/yyyy'
]);
$builder->get('myDate')->addModelTransformer(new CallbackTransformer(
function ($value) {
if(!$value) {
return new \DateTime('now +1 month');
}
return $value;
},
function ($value) {
return $value;
}
));
This solution applies the behaviour to just this form, it does not couple this behaviour to the entity itself. You might have several forms that modify an entity with different required behaviours. Some require a default date, others don't.
此解决方案仅将此行为应用于此表单,它不会将此行为耦合到实体本身。您可能有多种表单来修改具有不同所需行为的实体。有些需要默认日期,有些则不需要。
回答by Boopathi
You can set the attributes to on update CURRENT_TIMESTAMP and defualt to current time stamp will update the current time stamp automatically without updating through query
您可以将属性设置为 on update CURRENT_TIMESTAMP 并且默认为当前时间戳将自动更新当前时间戳,而无需通过查询更新
`feildname` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP