sajad torkamani

In a nutshell

The preUpdate event is called inside the EntityManager::flush() method, just before an SQL UPDATE statement.

It’s not triggered when the entity’s computed changeset is empty, nor for a DQL UPDATE statement.

You get an instance of PreUpdateEventArgs which has the following methods:

  • getEntity() to get access to the actual entity.
  • getEntityChangeSet() to get a copy of the changeset array. Changes to this returned array do not affect updating.
  • hasChangedField($fieldName) to check if the given field name of the current entity changed.
  • getOldValue($fieldName) and getNewValue($fieldName) to access the values of a field.
  • setNewValue($fieldName, $value) to change the value of a field to be updated.

Restrictions

  • Changes to associations of the passed entity aren’t recognized by the flush operation. So if you update an associated entity in the preUpdate lifecycle hook, the flush operation won’t take those changes into account.
  • Changes to fields of the passed entity aren’t recognised by the flush operation. Use the $eventArgs->setNewValue($field, $value) instead.
  • Calls to EntityManager::persist() and EntityManager::remove() are strongly discouraged and may not work as expected.

Sources/links

Tagged: Doctrine

Leave a comment

Your email address will not be published. Required fields are marked *