php Doctrine 2.1 - 日期时间列默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7698625/
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
Doctrine 2.1 - datetime column default value
提问by Krzysztof Trzos
Could someone tell me how to add default value on datetime column? I can't do this like this:
有人能告诉我如何在日期时间列上添加默认值吗?我不能这样做:
protected $registration_date = date("Y-m-d H:i:s", time());
So how?
又怎样?
回答by sanis
For default value CURRENT_TIMESTAMP:
对于默认值 CURRENT_TIMESTAMP:
@ORM\Column(name="created_at", type="datetime", options={"default": "CURRENT_TIMESTAMP"})
Or for older Symfony versions:
或者对于较旧的 Symfony 版本:
@ORM\Column(name="created_at", type="datetime", options={"default": 0})
Worked for me... However this works only with MySQL.
为我工作......但是这只适用于 MySQL。
回答by Alessandro Desantis
You can also use lifecycle callbacks if you want to be very precise:
如果你想非常精确,你也可以使用生命周期回调:
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\HasLifecycleCallbacks
* ...
*/
class MyEntity
{
/**
* @ORM\PrePersist
*/
public function onPrePersistSetRegistrationDate()
{
$this->registration_date = new \DateTime();
}
}
回答by Max
You map your property as DateTime type then set the value in the constructor using a new DateTime instance:
您将您的属性映射为 DateTime 类型,然后使用新的 DateTime 实例在构造函数中设置值:
/**
* @Entity
* @Table(name="...")
*/
class MyEntity
{
/** @Column(type="datetime") */
protected $registration_date;
public function __construct()
{
$this->registration_date = new DateTime();
}
}
This works as the constructor of a persisted class is not called upon hydration.
这是因为在水合时不会调用持久类的构造函数。
回答by rat4m3n
There is an extension for this automating this...
有一个扩展可以自动化这个......
https://github.com/l3pp4rd/DoctrineExtensions/blob/master/doc/timestampable.md
https://github.com/l3pp4rd/DoctrineExtensions/blob/master/doc/timestampable.md
/**
* @var \DateTime
*
* @ORM\Column(name="date_added", type="datetime")
* @Gedmo\Timestampable(on="create")
*/
private $date_added;
/**
* @var \DateTime
*
* @ORM\Column(name="date_modified", type="datetime")
* @Gedmo\Timestampable(on="update")
*/
private $date_modified;
回答by D.Samchuk
I think, the best way to accomplish autofill for datetime
is to make like that:
我认为,完成自动填充的最佳方法datetime
是这样制作:
* @ORM\Column(type="datetime", options={"default"="CURRENT_TIMESTAMP"})
Putting logic to constructor isn't right solution, because setting default values are SQL client responsibility. If you decide no longer use ORM - you will lost business logic. Plus, if using constructor you won't be able to add default timestamps to datetime
attributes for existing rows.
将逻辑放入构造函数不是正确的解决方案,因为设置默认值是SQL 客户端的责任。如果您决定不再使用 ORM - 您将丢失业务逻辑。另外,如果使用构造函数,您将无法datetime
为现有行的属性添加默认时间戳。
回答by nikhil_
@var string @ORM\Column(name="login_at", type="datetime", options={"default" = "CURRENT_TIMESTAMP"})
This will work. Just posting for future ref.
这将起作用。只是发布以供将来参考。
回答by sdespont
Work for me with MySql and Symfony 3.4.
使用 MySql 和 Symfony 3.4 为我工作。
...
fields:
start_date:
type: date
nullable: false
options:
default: '1910-01-01'
comment: 'The default date is 1910-01-01'
...