- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
The prePersist event occurs for a given entity before the respective EntityManager persist operation for that entity is executed. It should be noted that this event is only triggered on initial persist of an entity (i.e. it does not trigger on future updates).
最后一部分很重要,让我想知道;在这种情况下与 __contruct() 方法有什么不同吗?
假设我想设置实体的创建日期和时间 (createdAt
)。我可以通过生命周期回调来做到这一点:
/**
* @ORM\PrePersist
*/
public function setTimestamp()
{
$this->setCreatedAt(new \DateTime('now'));
}
或者我可以像这样使用构造函数:
public function __construct()
{
$this->createdAt = new \DateTime('now');
}
这完全一样吗?如果是这样,为什么会有 PrePersist
生命周期回调?
最佳答案
Does this do exactly the same?
不!它们在语义上是不同的。
考虑一种情况,您创建对象并处理一些数据(这将需要一段时间),然后将其保存到数据库中。所以时间可能会有所不同。
关于symfony - prePersist 和构造方法之间的 Doctrine 差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53519543/
我在继承和 方面遇到了一些问题@PrePersist 注解。 我的源代码如下所示: _带有带注释的 updateDates() 方法的“基”类: @javax.persistence.Entity @
我有 GenericEntity 类,其中包含 @PrePersist 方法 onCreate(),它在将实体保存到数据库之前设置时间戳。它运行良好,但当我尝试测试我的 DAO 类时,我遇到了问题。
我是 Symfony2 的新手,我想知道 prePersist 和 preUpdate 事件有什么区别。看起来 prePersist 在我“保留”记录之前被“触发”,但是 preUpdate 什么时候
我如何模拟 @PrePersist 方法,例如我实例化的实体的 preInit() ? 我正在使用 TestNG。 EasyMock 是首选。 @Test(enabled = true) public
我的实体的 prePersist() 方法没有被调用。 username = $username; return $this; } /** * Get use
我用这段线来合并: partner = em.merge(partner); 这是在我的实体中: @PrePersist public void updateModificationDate(){
我有以下用于将用户存储在数据库中的实体,目前它仍然是 WIP,但我在创建新用户时遇到重复问题。每次我使用注册表单和 prePersist 添加新用户时,它都会复制 Roles 表中的值。在 addUs
我正在使用 Hibernate 和 MySQL 在 Spring MVC 中开发一个应用程序,但我遇到了一个问题。我正在尝试使用 @PrePersist 注释在我的 Java 实体中填充我最后修改的字
我在实现 Doctrine EventListener 时遇到问题。创建新发票时,InvoiceType 表单中包含一组项目(标题、价格、金额)。对于发票,在 price 字段中,我想插入所有购买产品
假设以下代码片段使用 @PrePersist 和 @PreUpdate 注释和 Joined-type 继承: @Entity @Inheritance(strategy=InheritanceTyp
根据doctrine documentary : The prePersist event occurs for a given entity before the respective Entity
我的 Symfony2 项目中有一个 Doctrine2 实体(称之为实体 A)。该实体与项目中的另一个实体(称为实体 B)具有 ManyToOne 关系。 实体 A 的状态属性为“事件”或“非事件”
根据doctrine documentary : The prePersist event occurs for a given entity before the respective Entity
如果未手动设置,我想自动设置其中一个实体字段。有没有办法检查这个?这些字段有一个默认值,所以我不能简单地比较这个值。我想知道学说是否维持值(value)是否改变以及我是否可以访问该信息。 此外,Doc
我编写了一个 @PrePersist 方法,该方法在保留我的 User 对象之前调用。该方法的目的是为用户进行一种预订,因此其他人无法获取他的电子邮件地址。因此,当用户即将被保留时,我说,“为用户#x
我有这个“架构”的“发布”实体/表: @Id @GeneratedValue(strategy = GenerationType.AUTO) int id; @GeneratedValue(strat
我有一个事务性警报表和一个主警报类型表。我想在表中添加警报时发送一封电子邮件,所以我想我会使用 PrePersist。但是,在我的电子邮件中,我想包含一些包含在警报类型表中的信息。 我试图在 Aler
我的代码 @Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public class SiteMessage imple
我正在使用基本实体: @MappedSuperclass public class BaseEntity { private static final Logger L = LoggerFac
好吧,我是 symfony 的初学者,到目前为止,我开发了一个网站,用户可以在其中注册自己,并在登录后动态创建表单,但我的注册表单无法按预期工作。我的基本想法是当用户尝试注册自己时,预先检查他输入的客
我是一名优秀的程序员,十分优秀!