Doctrine preUpdate reference
26 June 2024 (Updated 26 June 2024)
On this page
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)
andgetNewValue($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, theflush
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()
andEntityManager::remove()
are strongly discouraged and may not work as expected.
Sources/links
Tagged:
Doctrine
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment