DDC-1020: Do we really need PreUpdateEventArgs::_assertValidField()? #1273

Closed
opened 2026-01-22 13:07:54 +01:00 by admin · 4 comments
Owner

Originally created by @doctrinebot on GitHub (Feb 8, 2011).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user @jwage:

I am not sure why we need this. I have a situation where we have a preUpdate() listener and the field I want to set the value for is not already in the changeset so _assertValidField() throws an exception. Do we really need this?

class Order
{
    private $something;
    private $tax;

    // ...
}

$order = $em->find('Order', 1);
$order->setSomething('test');

$em->flush();

class MyListener
{
    public function preUpdate(PreUpdateEventArgs $args)
    {
        $calculatedTax = 0;
        // _assertValidField() in PreUpdateEventArgs throws an exception because
        // tag is not already in the changeset. If i hack around it and do $this->tax = 1; or something
        // in the contructor of Order then it shows up in the changeset and setNewValue() works
        $args->setNewValue('tax', $calculatedTax);
    }
}
Originally created by @doctrinebot on GitHub (Feb 8, 2011). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user @jwage: I am not sure why we need this. I have a situation where we have a preUpdate() listener and the field I want to set the value for is not already in the changeset so _assertValidField() throws an exception. Do we really need this? ``` class Order { private $something; private $tax; // ... } $order = $em->find('Order', 1); $order->setSomething('test'); $em->flush(); class MyListener { public function preUpdate(PreUpdateEventArgs $args) { $calculatedTax = 0; // _assertValidField() in PreUpdateEventArgs throws an exception because // tag is not already in the changeset. If i hack around it and do $this->tax = 1; or something // in the contructor of Order then it shows up in the changeset and setNewValue() works $args->setNewValue('tax', $calculatedTax); } } ```
admin added the Improvement label 2026-01-22 13:07:54 +01:00
admin closed this issue 2026-01-22 13:07:55 +01:00
Author
Owner

@doctrinebot commented on GitHub (Feb 11, 2011):

Comment created by @jwage:

Working on an actual test and patch for this.

@doctrinebot commented on GitHub (Feb 11, 2011): Comment created by @jwage: Working on an actual test and patch for this.
Author
Owner

@doctrinebot commented on GitHub (Feb 11, 2011):

Comment created by @jwage:

After discussing this we discovered that you just need to modify the entity and then explicitly recompute the changeset for that document.

class DDC1020OrderEventListener
{
    public function preUpdate(LifecycleEventArgs $args)
    {
        $order = $args->getEntity();
        $order->tax = 100;
        $em = $args->getEntityManager();
        $uow = $em->getUnitOfWork();
        $uow->recomputeSingleEntityChangeSet(
            $em->getClassMetadata(get_class($order)),
            $args->getEntity()
        );
    }
}
@doctrinebot commented on GitHub (Feb 11, 2011): Comment created by @jwage: After discussing this we discovered that you just need to modify the entity and then explicitly recompute the changeset for that document. ``` class DDC1020OrderEventListener { public function preUpdate(LifecycleEventArgs $args) { $order = $args->getEntity(); $order->tax = 100; $em = $args->getEntityManager(); $uow = $em->getUnitOfWork(); $uow->recomputeSingleEntityChangeSet( $em->getClassMetadata(get_class($order)), $args->getEntity() ); } } ```
Author
Owner

@doctrinebot commented on GitHub (Feb 11, 2011):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (Feb 11, 2011): Issue was closed with resolution "Invalid"
Author
Owner

@mvorisek commented on GitHub (Jul 10, 2018):

preUpdate
Once all preUpdate listeners are invoked, the UoW::recomputeSingleEntityChangeSet() method is called implicitly.

For deeper analysis read this and see the Doctrine UoW code:
So the UoW::recomputeSingleEntityChangeSet() method needs to be called by the user only if there are more prePersist listeners and the changeSet needs to be the latest for every of them (but keep in mind that the prePersist listeners can be called in any order).

onFlush
Note: for some reasons the UoW::recomputeSingleEntityChangeSet() method is not called after onFlush listeners and invoked so in onFlush listeners it needs to be called manually!

@mvorisek commented on GitHub (Jul 10, 2018): **preUpdate** Once all preUpdate listeners are invoked, the UoW::recomputeSingleEntityChangeSet() method is called implicitly. For deeper analysis read this and see the Doctrine UoW code: So the UoW::recomputeSingleEntityChangeSet() method needs to be called by the user only if there are more prePersist listeners and the changeSet needs to be the latest for every of them (but keep in mind that the prePersist listeners can be called in any order). **onFlush** Note: for some reasons the UoW::recomputeSingleEntityChangeSet() method is not called after onFlush listeners and invoked so in onFlush listeners it needs to be called manually!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1273