1

I have an preUpdate Eventlistener on an Entitychild in my embedded Form.

I can change the attribute related to my entity:

    public function preUpdate(LifecycleEventArgs $eventArgs)
{   
    $entity = $eventArgs->getEntity();
    $em = $eventArgs->getEntityManager();

    if ($entity instanceof AOSupplierReference) {
        if ($eventArgs->hasChangedField('amount')) {

            $entity->setConfirmed(false);

        }
    }
}

But now I have to change an attribute of the parent Entity, this don't work in my preUpdate event:

$entity->getPurchaseOrder()->setStatus(4);

only the $entity->setConfirmed(false) changes.

ChrisS
  • 736
  • 2
  • 8
  • 21
  • Can you change the parent's value in your controller, prior to binding the form? – Tocacar Jul 23 '13 at 14:47
  • yes, but that is not my goal, because it depends on the preUpdate Child action, if I set the status of the purchaseOrder to 4. Furthermore I have access to the getPurchaseOrder() methods but the setStatus(4) is not set in the db – ChrisS Jul 23 '13 at 15:05

1 Answers1

3

You can't update a related entity in a preUpdate listener:

PreUpdate is the most restrictive to use event, since it is called right before an update statement is called for an entity inside the EntityManager#flush() method.

Changes to associations of the updated entity are never allowed in this event, since Doctrine cannot guarantee to correctly handle referential integrity at this point of the flush operation.

See the documentation.

Nicolai Fröhlich
  • 51,330
  • 11
  • 126
  • 130