How to access old values on PrePersist LifecycleCallback in Doctrine2(如何在 Doctrine2 中访问 PrePersist LifecycleCallback 上的旧值)
问题描述
我在 Doctrine2 中有一个实体,并将 HasLivecycleCallbacks 与 PrePersist 一起使用.一般来说,这很好用,但我只想在实体中的某些字段发生更改时更改版本.我有机会获得旧价值观吗?还是只是更改了键?
I have an entity in Doctrine2 and use the HasLivecycleCallbacks with PrePersist. In general this works fine, but I would like to change the version only, when certain fields in my entity change. Do I have a chance to get the old Values? Or just the keys that have been changed?
/**
* @ORMHasLifecycleCallbacks
*/
class Person {
/**
* @PrePersist
* @PreUpdate
*/
public function increaseVersion() {
if ( $this->version == null ) {
$this->version = 0;
}
// only do this, when a certain attribute changed
$this->version++;
}
}
推荐答案
这取决于我们说的是哪个 LifecycleEvent.PrePersist 和 PreUpdate 是不同的事件.
It depends on which LifecycleEvent we are talking about. PrePersist and PreUpdate are different events.
PreUpdate 在实体更新之前触发.这会给你一个 PreUpdateEventArgs
对象,它是一个扩展的 LifecycleEventArgs
对象.这将允许您查询更改的字段并允许您访问旧值和新值:
PreUpdate is fired before an Entity is, well, updated. This will give you a PreUpdateEventArgs
object, which is an extended LifecycleEventArgs
object. This will allow you to query for changed fields and give you access to the old and new value:
if ($event->hasChangedField('foo')) {
$oldValue = $event->getOldValue('foo');
$newValue = $event->getNewValue('foo');
}
您还可以通过 getEntityChangeSet()
获取所有更改的字段值,这将为您提供如下数组:
You could also get all the changed field values through getEntityChangeSet()
, which would give you an array like this:
array(
'foo' => array(
0 => 'oldValue',
1 => 'newValue'
),
// more changed fields (if any) …
)
另一方面,
PrePersist 假设一个新实体(想想插入新行).在 PrePersist 中,你会得到一个 LifecycleEventArgs
对象,它只能访问实体和 EntityManager
.理论上,您可以访问 UnitOfWork
(它跟踪对实体的所有更改)通过 EntityManager
,所以你可以尝试这样做
PrePersist, on the other hand, assumes a fresh Entity (think insert new row). In PrePersist, you'll get a LifecycleEventArgs
object which only has access to the Entity and the EntityManager
. In theory, you can get access to the UnitOfWork
(which keeps track of all the changes to Entities) through the EntityManager
, so you could try to do
$changeSet = $event->getEntityManager()->getUnitOfWork()->getEntityChangeSet(
$event->getEntity()
);
获取要持久化实体的更改.然后,您可以检查此数组是否有更改的字段.但是,由于我们谈论的是插入而不是更新,我假设所有字段都将被视为已更改",并且旧值可能全部为空.我不确定,这会根据您的需要工作.
to get the changes for the to be persisted Entity. You could then check this array for changed fields. However, since we are talking about an insert and not an update, I assume all fields wil be considered "changed" and the old values will likely be all null. I am not sure, this will work as you need it.
进一步参考:http://docs.doctrine-project.org/en/2.0.x/reference/events.html
这篇关于如何在 Doctrine2 中访问 PrePersist LifecycleCallback 上的旧值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Doctrine2 中访问 PrePersist LifecycleCallback 上的旧值
基础教程推荐
- 在多维数组中查找最大值 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01