Sorry to give you the wrong answer at first, this should lead you in the right direction (note that this is not ideal).
You will need to implement two events. Anyone who listens to the OnFlush event and acts as follows:
// This should listen to OnFlush events public function updateLastModifiedTime(OnFlushEventArgs $event) { $entity = $event->getEntity(); $entityManager = $event->getEntityManager(); $unitOfWork = $entityManager->getUnitOfWork(); if (count($unitOfWork->getScheduledEntityInsertions()) > 0 || count($unitOfWork->getScheduledEntityUpdates()) > 0) { // update the user here $this->user->setLastModifiedDate(new \DateTime()); } }
We need to wait for the OnFlush event, because this is the only opportunity for us to access all the work that will be done. Notice I did not include it above, but there is also $unitOfWork->getScheduledEntityDeletions() if you want to track this.
Then you need another last event listener that listens for the PostFlush event and looks like this:
// This should listen to PostFlush events public function writeLastUserUpdate(PostFlushEventArgs $event) { $entityManager = $event->getEntityManager(); $entityManager->persist($this->user); $entityManager->flush($this->user); }
Once the transaction has been started, it is too late, unfortunately, to get a doctrine to save another object. Because of this, we can do an update in the field of the User object in the OnFlush handler, but we cannot actually save it. (You can probably find a way to do this, but it is not supported by Doctrine and will have to use some of UnitOfWork's secure APIs.)
Once the transaction completes, you can immediately execute another quick transaction to update the date and time for the user. Yes, this has an unfortunate side effect that is not executed in a single transaction.
source share