Skip to content

Commit

Permalink
Fixing issue that doesn't track relationship changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Vison committed Apr 2, 2018
1 parent a73c8f7 commit a9c4d51
Showing 1 changed file with 22 additions and 43 deletions.
65 changes: 22 additions & 43 deletions EventListener/DoctrineEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit a9c4d51

Please sign in to comment.