原则 - 将默认时间戳添加到实体,如NOW() [英] Doctrine - Add default time stamp to entity like NOW()

查看:204
本文介绍了原则 - 将默认时间戳添加到实体,如NOW()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遵循教义指导原则,我了解如何为实体设置默认值,但如果我想要一个日期/时间戳,该怎么办?

Following the Doctrine guidelines I understand how to set a default value for an Entity, but what if I wanted a date/time stamp?

  • http://docs.doctrine-project.org/projects/doctrine-orm/en/2.1/reference/faq.html

我的问题是我的数据库默认为NOW ()在一个字段上,但是当我使用Doctrine插入一个记录时,值为null或空白,但插入的其余部分发生。

My problem is my database has a default of NOW() on a field but when I use Doctrine to insert a record the values are null or blank but the rest of the insert happened.

此外,由于Doctrine声明声明

Also since Doctrine says to declare the default as a const, this also creates a problem.

建议?

推荐答案

确定我找到解决方案:

  • https://doctrine-orm.readthedocs.org/en/latest/reference/php-mapping.html?highlight=callback
  • http://doctrine-orm.readthedocs.org/en/latest/reference/events.html#lifecycle-events

prePersist 选项是我在做的。

确保你在注释

<?php

/** @Entity 
 *  @HasLifecycleCallbacks 
 */
class User

这里是他们提供的功能示例



and here is the function example they offer

/** 
 *  @PrePersist 
 */
public function doStuffOnPrePersist()
{
    $this->createdAt = date('Y-m-d H:i:s');
}

如果你像我一样使用ORM

And if you're using ORM like I am

<?php

/** @ORM\Entity 
 *  @ORM\HasLifecycleCallbacks 
 */
class User

这里是他们提供的函数示例

and here is the function example they offer

/** 
 *  @ORM\PrePersist 
 */
public function doStuffOnPrePersist()
{
    $this->createdAt = date('Y-m-d H:i:s');
}

这篇关于原则 - 将默认时间戳添加到实体,如NOW()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆