From a9c4d511cff162d82a30934a7bea35ef588f4a45 Mon Sep 17 00:00:00 2001 From: Benjamin Vison Date: Mon, 2 Apr 2018 11:40:45 -0400 Subject: [PATCH] Fixing issue that doesn't track relationship changes --- EventListener/DoctrineEventSubscriber.php | 65 ++++++++--------------- 1 file changed, 22 insertions(+), 43 deletions(-) diff --git a/EventListener/DoctrineEventSubscriber.php b/EventListener/DoctrineEventSubscriber.php index 29ec183..e1b8577 100755 --- a/EventListener/DoctrineEventSubscriber.php +++ b/EventListener/DoctrineEventSubscriber.php @@ -25,66 +25,45 @@ public function getSubscribedEvents() 'preRemove', ); } - - public function getChangedOids($entities, &$oids) { - - foreach($entities as $entity) { - - $reflection = new \ReflectionObject($entity); - $methods = $reflection->getMethods(); - - foreach($methods as $method) { - - // Getter must not have a parameter!! - if(strpos($method->getName(), "get") !== false && count($method->getParameters()) <= 0) { - $result = $method->invoke($entity); - if(is_object($result)) { - $reflection2 = new \ReflectionClass(get_class($result)); - $annotations = $reflection2->getDocComment(); - if(strpos($annotations, '@ORM\Entity') !== false) { - $oid = spl_object_hash($result); - if(method_exists($result, 'setLastTimestamp')) { - $oids[] = $oid; - } - $this->getChangedOids(array($result), $oids); - } - } - } - } - } - } - + public function onFlush(OnFlushEventArgs $args) { $em = $args->getEntityManager(); $uow = $em->getUnitOfWork(); + $somethingChanged = false; - $oids = array(); - $this->getChangedOids($uow->getScheduledEntityUpdates(), $oids); + $identityMap = $uow->getIdentityMap(); - foreach ($uow->getScheduledEntityUpdates() as $keyEntity => $entity) { + foreach($identityMap as $map) { + foreach($map as $object) { + $changes = $uow->getEntityChangeSet($object); + if(count($changes) > 1 || (count($changes) > 0 && !isset($changes["lastTimestamp"]))) { + $somethingChanged = true; + break; + } + } + + if($somethingChanged) { + break; + } + } + foreach ($uow->getScheduledEntityUpdates() as $keyEntity => $entity) { $changes = $uow->getEntityChangeSet($entity); - if (count($changes) == 1 && isset($changes["lastTimestamp"])) { + if(count($changes) == 1 && isset($changes["lastTimestamp"]) && !$somethingChanged) { $oid = spl_object_hash($entity); - if(!in_array($oid, $oids)) { -// dump(get_class($entity)." is not really chaning anything..."); - $uow->clearEntityChangeSet($oid); - continue; - } + $uow->clearEntityChangeSet($oid); + } else { + $this->handleEntityChange($em, $entity); } - $this->handleEntityChange($em, $entity); } foreach ($uow->getScheduledEntityInsertions() as $keyEntity => $entity) { $this->handleEntityChange($em, $entity); } -// -// dump($oids); -// dump("Finished"); -// die; + } public function preRemove(LifecycleEventArgs $args) {