Update entity before flush to prevent a possible after flush

@param PreFlushEventArgs $args
12357
public function preFlush(PreFlushEventArgs $args)
    {
        $uow = $args->getEntityManager()->getUnitOfWork();
        $result = $uow->getIdentityMap();
        if(isset($result[$this->targetClass])) {
            foreach ($result[$this->targetClass] as $entity) {
                $this->updateEntity($entity);
            }
        }
    }
855885
public function postFlush(PostFlushEventArgs $eventArgs)
    {
        $em = $eventArgs->getEntityManager();
        $uow = $em->getUnitOfWork();
        foreach ($uow->getIdentityMap() as $entities) {
            foreach ($entities as $entity){
                $this->collectEventsFromEntity($entity);
            }
        }
    }
1318884
public function onFlush(OnFlushEventArgs $args): void
    {
        // Get the entity manager and unit of work.
        $em = $args->getEntityManager();
        $uow = $em->getUnitOfWork();

        // Check insertions for new events.
        foreach ($uow->getScheduledEntityInsertions() as $entity) {
            foreach ($this->getInvitations($entity) as $invitation) {
                $em->persist($invitation);
                $uow->computeChangeset($em->getClassMetadata(Invitation::class), $invitation);
            }
        }

        // Check updates for updated events.
        foreach ($uow->getScheduledEntityUpdates() as $entity) {
            foreach ($this->getInvitations($entity) as $invitation) {
                $em->persist($invitation);
                $uow->computeChangeset($em->getClassMetadata(Invitation::class), $invitation);
            }
        }
    }
910131
public function onFlush(OnFlushEventArgs $args)
    {
        $entityManager = $args->getEntityManager();

        $unitOfWork = $entityManager->getUnitOfWork();

        foreach ($unitOfWork->getScheduledEntityDeletions() as $entity) {

            $traits = class_uses(get_class($entity));

            if (in_array('BespokeSupport\CreatedUpdatedDeletedBundle\Traits\EntityIsDeletedTrait', $traits)) {
                /**
                 * @var $entity \BespokeSupport\CreatedUpdatedDeletedBundle\Traits\EntityIsDeletedTrait
                 */
                // hard delete
                if ($entity->isDeleted()) {
                    continue;
                }

                //soft delete
                $entity->setIsDeleted(true);

                $entityManager->persist($entity);
                $unitOfWork->scheduleExtraUpdate($entity, array('isDeleted'=> array(0, 1)));
            }

            if (in_array('BespokeSupport\CreatedUpdatedDeletedBundle\Traits\EntityDeletedTrait', $traits)) {
                /**
                 * @var $entity \BespokeSupport\CreatedUpdatedDeletedBundle\Traits\EntityDeletedTrait
                 */

                // hard delete
                if ($entity->getDeleted()) {
                    continue;
                }

                //soft delete
                $entity->setDeleted(new \DateTime());
                $entityManager->persist($entity);
                $unitOfWork->scheduleExtraUpdate($entity, array('deleted'=> array(null, new \DateTime())));
            }
        }
    }
278536
public function beforeUpdate(array &$args) {
        $data = &$args[1];
        $data[$this->_fields['modifiedField']] = date(self::DATE_FORMAT);
    }
374962
public function onFlush($args)
    {
        if (!$this->isDirty) {
            /** @var $em \Doctrine\ORM\EntityManager */
            $em = $args->getEntityManager();

            /** @var $uow \Doctrine\ORM\UnitOfWork */
            $uow = $em->getUnitOfWork();

            $array = array(
                $uow->getScheduledEntityUpdates(),
                $uow->getScheduledEntityDeletions(),
                $uow->getScheduledEntityInsertions(),
                $uow->getScheduledCollectionUpdates(),
                $uow->getScheduledCollectionDeletions(),
                $uow->getScheduledCollectionDeletions(),
                $uow->getScheduledCollectionUpdates()
            );

            foreach ($array as $obj) {
                foreach ($obj as $element) {
                    if ($element instanceof Entity\Message
                     || $element instanceof Entity\MessageTranslation
                    ) {
                        $this->isDirty = true;
                        break 2;
                    }
                }
            }
        }

        $this->flushCache();
    }
1362662
public function beforeUpdate(array &$args) {
        $model = $args[0];
        $data = &$args[1];
        $where = $args[2];

        // When updating, it's quite possible {$this->_column} is not in $data.
        // If so, collect it live.
        if (empty($data[$this->_column])) {
            $row = $model->fetchRow($where);
            if ($row->{$this->_column}) {
                $data[$this->_column] = $row->{$this->_column};
            }
        }
        $this->_setBitlyUrl($data);
    }
176838
public function postUpdate(LifecycleEventArgs $args)
    {
        if ($args->getObject() instanceof BlockInterface) {
            $this->getService($args)->postUpdate($args->getObject());
        }
    }
403720
public function onFlush(OnFlushEventArgs $args): void
    {
        $uow = $args->getEntityManager()->getUnitOfWork();

        foreach ($uow->getScheduledEntityDeletions() as $entity) {
            $class = ClassUtils::getClass($entity);

            if ($this->getSharingManager()->hasSubjectConfig($class)) {
                $subject = SubjectIdentity::fromObject($entity);
                $this->deleteSharingSubjects[$subject->getType()][] = $subject->getIdentifier();
            } elseif ($this->getSharingManager()->hasIdentityConfig($class)) {
                $subject = SubjectIdentity::fromObject($entity);
                $this->deleteSharingIdentities[$subject->getType()][] = $subject->getIdentifier();
            }
        }
    }
1312424
public function postLoad(LifecycleEventArgs $args) {
        $entity = $args->getEntity();
        if (!$this->hasInDecodedRegistry($entity, $args->getEntityManager())) {
            if ($this->processFields($entity, false)) {
                $this->addToDecodedRegistry($entity, $args->getEntityManager());
            }
        }
    }
63049
public function beforeInsert(array &$args) {
        $data = &$args[1];
        foreach ($this->_fields as $field) {
            if (array_key_exists($field, $data)) {
                $data[$field] = $this->filter($data[$field], $this->getConfig());
            }
        }
    }
192936
public function preUpdate(PreUpdateEventArgs $eventArgs)
    {
        $entity = $eventArgs->getEntity();
        if ($entity instanceof CategoryInterface) {
            $this->removeParentCategoryForRootCategory($entity);
        }

        return $this;
    }
796321
public function onFlush(OnFlushEventArgs $eventArgs)
    {
        if (!$this->eventEmittingService->isEnabled()) {
            return;
        }

        $entityManager = $eventArgs->getEntityManager();
        $unitOfWork = $entityManager->getUnitOfWork();

        foreach ($unitOfWork->getScheduledEntityInsertions() as $entity) {
            $className = get_class($entity);
            if (isset($this->monitorEntitiesSetting[$className])) {
                $entityMonitoringConfiguration = $this->monitorEntitiesSetting[$className];

                if (isset($entityMonitoringConfiguration['events']['created'])) {
                    $data = [];
                    foreach ($entityMonitoringConfiguration['data'] as $key => $eelExpression) {
                        $data[$key] = Utility::evaluateEelExpression($eelExpression, $this->eelEvaluator, ['entity' => $entity]);
                    }

                    $event = $this->eventEmittingService->emit($entityMonitoringConfiguration['events']['created'], $data);
                    $unitOfWork->computeChangeSet($entityManager->getClassMetadata(Event::class), $event);
                }
            }
        }

        foreach ($unitOfWork->getScheduledEntityDeletions() as $entity) {
            $className = get_class($entity);
            if (isset($this->monitorEntitiesSetting[$className])) {
                $entityMonitoringConfiguration = $this->monitorEntitiesSetting[$className];

                if (isset($entityMonitoringConfiguration['events']['deleted'])) {
                    $data = [];
                    foreach ($entityMonitoringConfiguration['data'] as $key => $eelExpression) {
                        $data[$key] = Utility::evaluateEelExpression($eelExpression, $this->eelEvaluator, ['entity' => $entity]);
                    }

                    $event = $this->eventEmittingService->emit($entityMonitoringConfiguration['events']['deleted'], $data);
                    $unitOfWork->computeChangeSet($entityManager->getClassMetadata(Event::class), $event);
                }
            }
        }
    }
1354358
public function onFlush(OnFlushEventArgs $eventArgs)
    {
        $em = $eventArgs->getEntityManager();
        $uow = $em->getUnitOfWork();

        foreach ($uow->getScheduledEntityInsertions() as $entity) {
            if ($entity instanceof FileInterface){
                $this->populateMeta($em, $uow, $entity);
                $this->scheduledForCopyFiles[spl_object_hash($entity)] = $entity;
            }
        }

        foreach ($uow->getScheduledEntityUpdates() as $entity) {

            if ($entity instanceof FileInterface){

                if ($this->versionEnabled){
                    $em->lock($entity, LockMode::OPTIMISTIC, $entity->getCurrentVersion());
                }
                
                if (true === $entity->isScheduledForDeletion()){
                    $uow->scheduleForDelete($entity);
                    continue;
                }
                
                $changeSet = $uow->getEntityChangeSet($entity);

                if (isset($changeSet['name'])){
                    // remove old file
                    $clonedEntity = clone $entity;
                    $clonedEntity->setName($changeSet['name'][0]);
                    $this->scheduledForDeleteFiles[spl_object_hash($clonedEntity)] = $clonedEntity;
                    $this->scheduledForCopyFiles[spl_object_hash($entity)] = $entity;
                    $this->checksum($entity);
                } 

                if(isset($changeSet['name']) || isset($changeSet['hash'])){
                    $this->populateMeta($em, $uow, $entity);
                }                
            }
        }

        foreach ($uow->getScheduledEntityDeletions() as $entity) {
            if ($entity instanceof FileInterface){
                $this->scheduledForDeleteFiles[spl_object_hash($entity)] = $entity;
            }
        }
    }
1107619
public function preFlushModifiedAt(\Doctrine\ORM\Event\PreFlushEventArgs $args) : self
    {
        if (is_null($this->getModifiedAt())) {
            $this->setModifiedAt(new \DateTime('now'));
        }

        return $this;
    }
1252264
public function onFlush(OnFlushEventArgs $args): void
    {
        $uow = $args->getEntityManager()->getUnitOfWork();
        $collection = $this->getAllCollections($uow);
        $invalidates = [];

        // check all scheduled insertions
        foreach ($collection as $object) {
            $invalidate = $this->invalidateCache($uow, $object);

            if (\is_string($invalidate)) {
                $invalidates[] = $invalidate;
            }
        }

        $this->flushCache(array_unique($invalidates));
    }
412971
public function onFlush(OnFlushEventArgs $args)
    {
        $em = $args->getEntityManager();

        $unitOfWork = $em->getUnitOfWork();

        foreach ($unitOfWork->getScheduledEntityUpdates() as $entity) {
            if ($entity instanceof PageInterface) {
                if ($contentRoute = $entity->getContentRoute()) {
                    $contentRoute->setPath(PageHelper::getPageRoutePath($entity->getPath()));
                    $em->persist($contentRoute);
                    $unitOfWork->computeChangeSet($em->getClassMetadata(get_class($contentRoute)), $contentRoute);
                    foreach ($entity->getAllChildren() as $child) {
                        $contentRoute = $child->getContentRoute();
                        $contentRoute->setPath(PageHelper::getPageRoutePath($child->getPath()));
                        $em->persist($contentRoute);
                        $unitOfWork->computeChangeSet($em->getClassMetadata(get_class($contentRoute)), $contentRoute);

                        if ($entity->getStatus() == Page::STATUS_PUBLISHED) {
                            if ($childSnapshot = $child->getSnapshot()) {
                                $snapshotRoute = $childSnapshot->getContentRoute();

                                $newPath = PageHelper::getPageRoutePath($child->getPath());

                                $snapshotRoute->setPath($newPath);
                                $childSnapshot->setPath($newPath);

                                $em->persist($childSnapshot);
                                $em->persist($snapshotRoute);

                                $unitOfWork->computeChangeSet(
                                    $em->getClassMetadata(get_class($childSnapshot)),
                                    $childSnapshot
                                );
                                $unitOfWork->computeChangeSet(
                                    $em->getClassMetadata(get_class($snapshotRoute)),
                                    $snapshotRoute
                                );
                            }
                        }
                    }
                }
            }
        }
    }