/**
  * Pre Persist event callback
  *
  * Checks if the entity contains an @Tracked (or derived)
  * annotation. If so, it will dispatch 'Events::ENTITY_CHANGED'
  * with the new entity states.
  *
  * @param LifecycleEventArgs $event
  */
 public function prePersist(LifecycleEventArgs $event)
 {
     $em = $event->getEntityManager();
     $entity = $event->getEntity();
     if (false === $this->meta_annotation_provider->isTracked($em, $entity)) {
         return;
     }
     $mutated_fields = $this->meta_mutation_provider->getMutatedFields($em, $entity, null);
     $this->logger->debug('Going to notify a change (prePersist) 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, null, $mutated_fields));
 }
 /**
  * @depends testCreateOriginalEntity
  */
 public function testGetMutatedFields()
 {
     // Simple new entity
     $venus = new Painting('The birth of Aphrodite');
     self::assertSame(['id', 'name'], $this->provider->getMutatedFields($this->em, $venus, null));
     self::assertSame([], $this->provider->getMutatedFields($this->em, $venus, $venus));
     // Complex entity
     $start = new Node('start');
     $start->id = 1;
     $end = new Node('end');
     $end->id = 10;
     $end->parent = $start;
     self::assertSame(['id', 'name', 'parent', 'mirror'], $this->provider->getMutatedFields($this->em, $end, null));
     self::assertSame([], $this->provider->getMutatedFields($this->em, $end, $end));
     $original = clone $end;
     $middle = new Node('middle');
     $middle->id = 5;
     $end->parent = $middle;
     $middle->parent = $start;
     self::assertSame(['parent'], $this->provider->getMutatedFields($this->em, $end, $original));
 }
 /**
  * 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));
             }
         }
     }
 }
 public function testGetMutatedFieldsEmpty()
 {
     $entity = new MockEntity();
     $original = null;
     $metadata = $this->buildMetadata($entity, [], ['parent']);
     $this->em->expects($this->exactly(1))->method('getClassMetadata')->willReturn($metadata);
     $provider = new EntityMutationMetadataProvider($this->reader);
     $this->assertEquals(["parent"], $provider->getMutatedFields($this->em, $entity, $original));
 }