/** * Handle onFlush event * * @param OnFlushEventArgs $event */ public function handleOnFlush(OnFlushEventArgs $event) { $em = $event->getEntityManager(); $uow = $em->getUnitOfWork(); $newEntities = $uow->getScheduledEntityInsertions(); foreach ($newEntities as $entity) { if ($entity instanceof Call) { $hasChanges = $this->activityManager->addActivityTarget($entity, $entity->getOwner()); // recompute change set if needed if ($hasChanges) { $uow->computeChangeSet($em->getClassMetadata(ClassUtils::getClass($entity)), $entity); } } } $changedEntities = $uow->getScheduledEntityUpdates(); foreach ($changedEntities as $entity) { if ($entity instanceof Call) { $hasChanges = false; $changeSet = $uow->getEntityChangeSet($entity); foreach ($changeSet as $field => $values) { if ($field === 'owner') { list($oldValue, $newValue) = $values; if ($oldValue !== $newValue) { $hasChanges |= $this->activityManager->replaceActivityTarget($entity, $oldValue, $newValue); } break; } } // recompute change set if needed if ($hasChanges) { $uow->computeChangeSet($em->getClassMetadata(ClassUtils::getClass($entity)), $entity); } } } }
public function testReplaceActivityTargetNoChanges() { $activityEntity = $this->getMock('Oro\\Bundle\\ActivityBundle\\Model\\ActivityInterface'); $oldTargetEntity = new Target(1); $newTargetEntity = new Target(2); $activityEntity->expects($this->exactly(2))->method('supportActivityTarget')->will($this->returnValueMap([[get_class($oldTargetEntity), true], [get_class($newTargetEntity), true]])); $activityEntity->expects($this->exactly(2))->method('hasActivityTarget')->will($this->returnValueMap([[$oldTargetEntity, false], [$newTargetEntity, true]])); $activityEntity->expects($this->never())->method('removeActivityTarget'); $activityEntity->expects($this->never())->method('addActivityTarget'); $this->assertFalse($this->manager->replaceActivityTarget($activityEntity, $oldTargetEntity, $newTargetEntity)); }