Symfony2-主义-更新后无变更集 [英] Symfony2 - Doctrine - no changeset in post update
问题描述
因此,当实体上的某个值更改时,我正在发送电子邮件。我只希望在更新后发送电子邮件,以防万一更新由于某种原因而失败。所以在preUpdate上我可以这样做
So i am sending an email when a certain value on an entity is changed. I only want the email to send after the update in case the update fails for what ever reason. so on the preUpdate I can do this
public function preUpdate(LifecycleEventArgs $args){
if ($args->hasChangedField('value') && is_null($args->getOldValue('value'))) {
$this->sendEmail();
}
}
但是我需要这样做postUpdate以及这些方法在postUpdate上不可用,因此我将其重构为:
but i need to do this on postUpdate and as these methods are not available on postUpdate i refactored it to look like this:
public function postUpdate(LifecycleEventArgs $args){
$entity = $args->getEntity();
$changeSet = $args->getEntityManager()->getUnitOfWork()->getEntityChangeSet($entity);
if ($entity instanceof Entity && isset( $changeSet['value'] ) && empty( $changeSet['value'][0] )) {
$this->sendEmail();
}
}
但是,这将返回空的更改集,但更改有已完成,可以在preUpdate中看到。谁能看到我做错了吗?帮助将不胜感激:)
However this returns an empty change set, but changes have been made and can be seen in preUpdate. Can anyone see what i am doing wrong? help would be much appreciated :)
推荐答案
在 preUpdate
事件中类 PreUpdateEventArgs
的事件对象,其中已为实体设置了更改。
On preUpdate
event you get event object of class PreUpdateEventArgs
where You have change set for entity.
在 postUpdate
上,您只获得类 LifecycleEventArgs
您仅可以要求提供更新的实体(并获取其最新状态)。
On postUpdate
you just get event object of class LifecycleEventArgs
where you can ask only for Updated entity (and get latest state of it).
如果要使用变更集,则需要在实际更新实体之前进行( preUpdate
事件)。
If you want to play with changeset then you need to do it before actual updating entity (preUpdate
event).
这篇关于Symfony2-主义-更新后无变更集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!