如何在 preUpdate 事件侦听器中更改字段的值? [英] How do I change a field's value in preUpdate event listener?
问题描述
文档说:
刷新操作不再识别对传递实体字段的更改,请使用传递给事件的计算更改集来修改原始字段值.
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.
但它也说:
getEntityChangeSet() 获取变更集数组的副本.对此返回数组的更改不影响更新.
getEntityChangeSet() to get a copy of the changeset array. Changes to this returned array do not affect updating.
这是否意味着我无法在 preUpdate 事件侦听器中更改实体的字段?如果没有,我将如何完成此更新?
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屋!