原则 2.1 - 日期时间列默认值

发布于 2024-12-08 17:21:05 字数 140 浏览 0 评论 0原文

有人可以告诉我如何在日期时间列上添加默认值吗?我不能这样做:

protected $registration_date = date("Y-m-d H:i:s", time());

那么我该如何处理呢?

Could someone tell me how to add the default value on a DateTime column? I can't do it like this:

protected $registration_date = date("Y-m-d H:i:s", time());

So how can I handle it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

£冰雨忧蓝° 2024-12-15 17:21:05

对于默认值 CURRENT_TIMESTAMP:

     @ORM\Column(name="created_at", type="datetime", options={"default": "CURRENT_TIMESTAMP"})

或者对于较旧的 Symfony 版本:

     @ORM\Column(name="created_at", type="datetime", options={"default": 0})

为我工作...但是,这仅适用于 MySQL

For default value CURRENT_TIMESTAMP:

     @ORM\Column(name="created_at", type="datetime", options={"default": "CURRENT_TIMESTAMP"})

Or for older Symfony versions:

     @ORM\Column(name="created_at", type="datetime", options={"default": 0})

Worked for me... However this works only with MySQL.

喜爱皱眉﹌ 2024-12-15 17:21:05

您将属性映射为 DateTime 类型,然后使用新的 DateTime 实例在构造函数中设置值:

/**
 * @Entity
 * @Table(name="...")
 */
class MyEntity
{
    /** @Column(type="datetime") */
    protected $registration_date;

    public function __construct()
    {
        $this->registration_date = new DateTime(); 
    }
}

这是因为在水合作用时不会调用持久类的构造函数。

You map your property as DateTime type then set the value in the constructor using a new DateTime instance:

/**
 * @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.

零度℉ 2024-12-15 17:21:05

如果您想要非常精确,您还可以使用生命周期回调:

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\HasLifecycleCallbacks
 * ...
 */
class MyEntity
{
    /**
     * @ORM\PrePersist
     */
    public function onPrePersistSetRegistrationDate()
    {
        $this->registration_date = new \DateTime();
    }
}

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();
    }
}
灯角 2024-12-15 17:21:05

我认为,完成日期时间自动填充的最佳方法是这样:

* @ORM\Column(type="datetime", options={"default"="CURRENT_TIMESTAMP"})

将逻辑放入构造函数不是正确的解决方案,因为设置默认值是SQL客户端的责任。如果您决定不再使用 ORM - 您将失去业务逻辑。另外,如果使用构造函数,您将无法向现有行datetime属性添加默认时间戳。

I think, the best way to accomplish autofill for datetime is to make like that:

* @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.

小忆控 2024-12-15 17:21:05

有一个扩展可以自动执行此操作...

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;

There is an extension for this automating this...

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;
你げ笑在眉眼 2024-12-15 17:21:05
@var string @ORM\Column(name="login_at", type="datetime", options={"default" = "CURRENT_TIMESTAMP"})

这会起作用。只是发布供将来参考。

@var string @ORM\Column(name="login_at", type="datetime", options={"default" = "CURRENT_TIMESTAMP"})

This will work. Just posting for future ref.

彩扇题诗 2024-12-15 17:21:05

为我使用 MySql 和 Symfony 3.4。

...
fields:
    start_date:
        type: date
        nullable: false
        options:
            default: '1910-01-01'
            comment: 'The default date is 1910-01-01'
...

Work for me with MySql and Symfony 3.4.

...
fields:
    start_date:
        type: date
        nullable: false
        options:
            default: '1910-01-01'
            comment: 'The default date is 1910-01-01'
...
清浅ˋ旧时光 2024-12-15 17:21:05

对于 doctrine-bundle: 2.10 和 PHP 属性,设置默认值:

#[ORM\Column(options: ['default' => 0])]
private bool $isActive = false;

For doctrine-bundle: 2.10 and PHP Attributes, setting a default value:

#[ORM\Column(options: ['default' => 0])]
private bool $isActive = false;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文