Updating a not modified field in preUpdate event #6900

Open
opened 2026-01-22 15:40:56 +01:00 by admin · 5 comments
Owner

Originally created by @mmarton on GitHub (Jan 9, 2022).

Bug Report

Hi!

this is either a documentation issue or a code one or a support question :D

Q A
BC Break no
Version 2.10

Summary

I want to change a field that in preUpdate event that is not in the changeset.

public function preUpdate(Investment $investment, PreUpdateEventArgs $args): void
{
    if (!$args->hasChangedField('reviewed')) {
        $investment->setReviewed(false);
    }
}

Current behavior

https://github.com/doctrine/orm/blob/2.10.x/docs/en/reference/events.rst#preupdate
The docs sais at the limitations part:

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, e.g. use $eventArgs->setNewValue($field, $value); as in the Alice to Bob example above.

I can't use the setNewValue method because it only works for values that are in the computed changeset.

But setting it on the entity works... so

  • is this a misdocumented behavior?
  • is it a code issue?
  • If I'm not suppose to do it this way, how should I?
Originally created by @mmarton on GitHub (Jan 9, 2022). ### Bug Report Hi! this is either a documentation issue or a code one or a support question :D | Q | A |------------ | ------ | BC Break | no | Version | 2.10 #### Summary I want to change a field that in preUpdate event that is not in the changeset. ```php public function preUpdate(Investment $investment, PreUpdateEventArgs $args): void { if (!$args->hasChangedField('reviewed')) { $investment->setReviewed(false); } } ``` #### Current behavior https://github.com/doctrine/orm/blob/2.10.x/docs/en/reference/events.rst#preupdate The docs sais at the limitations part: > 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, e.g. use $eventArgs->setNewValue($field, $value); as in the Alice to Bob example above. I can't use the `setNewValue` method because it only works for values that are in the computed changeset. But setting it on the entity works... so - is this a misdocumented behavior? - is it a code issue? - If I'm not suppose to do it this way, how should I?
Author
Owner

@teamore commented on GitHub (Jan 10, 2022):

Same issue here. It seems that you can only modify fields that are already scheduled for Update when using the PreUpdate-hook provided by doctrine. Changes to the respective entity by $entity->setField() are ignored whereas $eventArgs->setNewValue($field, $value) results in an error if the field has not already been changed before the listener is called.

I would also appreciate an answer to this issue/lack of clarity.

@teamore commented on GitHub (Jan 10, 2022): Same issue here. It seems that you can only modify fields that are already scheduled for Update when using the PreUpdate-hook provided by doctrine. Changes to the respective entity by $entity->setField() are ignored whereas $eventArgs->setNewValue($field, $value) results in an error if the field has not already been changed before the listener is called. I would also appreciate an answer to this issue/lack of clarity.
Author
Owner

@teamore commented on GitHub (Jan 10, 2022):

I found out that when I recalculate the ChangeSet (from UnitOfWork) after having applied the desired changes to the entity, the update also includes the changes applied inside the PreUpdate-listener.

Therefore, if we are dealing with a doctrine document (ODM), you need to recalculate the UOW-ChangeSet after setting the value for the field via:

$om = $args->getObjectManager(); $om->getUnitOfWork()->recomputeSingleDocumentChangeSet($om->getClassMetadata($entity::class), $entity);

@teamore commented on GitHub (Jan 10, 2022): I found out that when I recalculate the ChangeSet (from UnitOfWork) after having applied the desired changes to the entity, the update also includes the changes applied inside the PreUpdate-listener. Therefore, if we are dealing with a doctrine document (ODM), you need to recalculate the UOW-ChangeSet after setting the value for the field via: ` $om = $args->getObjectManager(); $om->getUnitOfWork()->recomputeSingleDocumentChangeSet($om->getClassMetadata($entity::class), $entity); `
Author
Owner

@mmarton commented on GitHub (Jan 26, 2022):

@beberlei can you help me with this question? :)

@mmarton commented on GitHub (Jan 26, 2022): @beberlei can you help me with this question? :)
Author
Owner

@mmarton commented on GitHub (May 9, 2022):

Anyone can help me out with this?

@mmarton commented on GitHub (May 9, 2022): Anyone can help me out with this?
Author
Owner

@EwenQuim commented on GitHub (Jun 5, 2023):

I'd like to know if any solution has been found since last year ! Thanks

@EwenQuim commented on GitHub (Jun 5, 2023): I'd like to know if any solution has been found since last year ! Thanks
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6900