如何在preUpdate事件侦听器中更改字段的值? [英] How do I change a field's value in preUpdate event listener?

查看:87
本文介绍了如何在preUpdate事件侦听器中更改字段的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件说:


已更新的实体的字段的更改不再被刷新操作识别,使用计算的更改集传递给事件以修改原始字段值。


但它也说:



getCntetChangeSet()获取一个更改集数组的副本


对此返回的数组的更改不会影响更新。


这是否意味着我无法更改preUpdate事件侦听器中的实体的字段?如果没有,我将如何完成此更新?

解决方案

显然,您需要自己重新计算更改集,以进行更改效果:

  $ em = $ args-> getEntityManager(); 
$ uow = $ em-> getUnitOfWork();
$ meta = $ em-> getClassMetadata(get_class($ entity));
$ uow-> recomputeSingleEntityChangeSet($ meta,$ entity);


Documentation says:

Changes to fields of the passed entities are not recognized by the flush operation anymore, use the computed change-set passed to the event to modify primitive field values.

But it also says:

getEntityChangeSet() to get a copy of the changeset array. Changes to this returned array do not affect updating.

Does this mean I can not change fields of an entity in preUpdate event listener? If not, how would I go about accomplishing this update?

解决方案

Apparently you need to recompute the changeset yourself for the changes to take effect:

$em = $args->getEntityManager();
$uow = $em->getUnitOfWork();
$meta = $em->getClassMetadata(get_class($entity));
$uow->recomputeSingleEntityChangeSet($meta, $entity);

这篇关于如何在preUpdate事件侦听器中更改字段的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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