/**
  * Pre Flush event callback
  *
  * Checks if the entity contains an @Tracked (or derived)
  * annotation. If so, it will attempt to calculate changes
  * made and dispatch 'Events::ENTITY_CHANGED' with the current
  * and original entity states. Note that the original entity
  * is not managed.
  *
  * @param PreFlushEventArgs $event
  */
 public function preFlush(PreFlushEventArgs $event)
 {
     $em = $event->getEntityManager();
     $changes = $this->meta_mutation_provider->getFullChangeSet($em);
     foreach ($changes as $updates) {
         if (0 === count($updates)) {
             continue;
         }
         if (false === $this->meta_annotation_provider->isTracked($em, current($updates))) {
             continue;
         }
         foreach ($updates as $entity) {
             if (!$this->meta_mutation_provider->isEntityManaged($em, $entity) || $entity instanceof Proxy && !$entity->__isInitialized()) {
                 continue;
             }
             $original = $this->meta_mutation_provider->createOriginalEntity($em, $entity);
             // New entities are handled in the pre-persist event.
             if (!$original) {
                 continue;
             }
             $mutated_fields = $this->meta_mutation_provider->getMutatedFields($em, $entity, $original);
             if (!empty($mutated_fields)) {
                 $this->logger->debug('Going to notify a change (preFlush) to {entity_class}, which has {mutated_fields}', ['entity_class' => get_class($entity), 'mutated_fields' => $mutated_fields]);
                 $em->getEventManager()->dispatchEvent(Events::ENTITY_CHANGED, new EntityChangedEvent($em, $entity, $original, $mutated_fields));
             }
         }
     }
 }
 public function testCreateOriginalEntityIdentity()
 {
     $gallery = new Gallery('Riverstreet 12');
     $gallery->addVisitor('Foo de Bar');
     $this->em->persist($gallery);
     $this->em->flush();
     $gallery->addVisitor('Bar Baz');
     $this->em->getUnitOfWork()->computeChangeSets();
     $this->em->flush();
     $original = $this->provider->createOriginalEntity($this->em, $gallery);
     self::assertSame($gallery->getId(), $original->getId());
 }
 public function testCreateOriginalEntityEmpty()
 {
     $entity = new MockEntity();
     $metadata = $this->buildMetadata($entity, ['id'], ['parent']);
     $this->uow->expects($this->once())->method('getOriginalEntityData')->willReturn([]);
     $this->em->expects($this->once())->method('getClassMetadata')->willReturn($metadata);
     $provider = new EntityMutationMetadataProvider($this->reader);
     $this->assertEquals(null, $provider->createOriginalEntity($this->em, $entity));
 }
 /**
  * Pre Flush event callback
  *
  * Checks if the entity contains an @Tracked (or derived)
  * annotation. If so, it will attempt to calculate changes
  * made and dispatch 'Events::entityChanged' with the current
  * and original entity states. Note that the original entity
  * is not managed.
  *
  * @param PreFlushEventArgs $event
  */
 public function preFlush(PreFlushEventArgs $event)
 {
     $em = $event->getEntityManager();
     $changes = $this->meta_mutation_provider->getFullChangeSet($em);
     foreach ($changes as $updates) {
         if (empty($updates)) {
             continue;
         }
         if (false === $this->meta_annotation_provider->isTracked($em, current($updates))) {
             continue;
         }
         foreach ($updates as $entity) {
             if (!$this->meta_mutation_provider->isEntityManaged($em, $entity) || $entity instanceof Proxy) {
                 continue;
             }
             $original = $this->meta_mutation_provider->createOriginalEntity($em, $entity);
             $mutated_fields = $this->meta_mutation_provider->getMutatedFields($em, $entity, $original);
             if (!empty($mutated_fields)) {
                 $this->logger->info('Going to notify a change to {entity_class}, which has {mutated_fields}', ['entity_class' => get_class($entity), 'mutated_fields' => $mutated_fields]);
                 $em->getEventManager()->dispatchEvent(Events::entityChanged, new EntityChangedEvent($em, $entity, $original, $mutated_fields));
             }
         }
     }
 }