/** * @param OnFlushEventArgs $args */ public function onFlush(OnFlushEventArgs $args) { $this->initializeFromEventArgs($args); $entities = array_merge($this->uow->getScheduledEntityInsertions(), $this->uow->getScheduledEntityDeletions(), $this->uow->getScheduledEntityUpdates()); /** @var Opportunity[] $entities */ $entities = array_filter($entities, function ($entity) { return 'OroCRM\\Bundle\\SalesBundle\\Entity\\Opportunity' === ClassUtils::getClass($entity); }); foreach ($entities as $entity) { if (!$entity->getId() && $this->isValuable($entity)) { // handle creation, just add to prev lifetime value and recalculate change set $b2bCustomer = $entity->getCustomer(); $b2bCustomer->setLifetime($b2bCustomer->getLifetime() + $entity->getCloseRevenue()); $this->scheduleUpdate($b2bCustomer); $this->uow->computeChangeSet($this->em->getClassMetadata(ClassUtils::getClass($b2bCustomer)), $b2bCustomer); } elseif ($this->uow->isScheduledForDelete($entity) && $this->isValuable($entity)) { $this->scheduleUpdate($entity->getCustomer()); } elseif ($this->uow->isScheduledForUpdate($entity)) { // handle update $changeSet = $this->uow->getEntityChangeSet($entity); if ($this->isChangeSetValuable($changeSet)) { if (!empty($changeSet['customer']) && $changeSet['customer'][0] instanceof B2bCustomer && B2bCustomerRepository::VALUABLE_STATUS === $this->getOldStatus($entity, $changeSet)) { // handle change of b2b customer $this->scheduleUpdate($changeSet['customer'][0]); } if ($this->isValuable($entity, isset($changeSet['closeRevenue'])) || B2bCustomerRepository::VALUABLE_STATUS === $this->getOldStatus($entity, $changeSet) && $entity->getCustomer()) { $this->scheduleUpdate($entity->getCustomer()); } } } } }
/** * On flush * * @param OnFlushEventArgs $args */ public function onFlush(OnFlushEventArgs $args) { $this->manager = $args->getEntityManager(); $this->uow = $this->manager->getUnitOfWork(); foreach ($this->uow->getScheduledEntityUpdates() as $entity) { $this->preUpdate($entity); } foreach ($this->uow->getScheduledEntityDeletions() as $entity) { $this->preRemove($entity); } }
/** * @return array|CustomerIdentityInterface[] */ protected function getChangedTrackedEntities() { $entities = array_merge($this->uow->getScheduledEntityInsertions(), $this->uow->getScheduledEntityDeletions(), $this->uow->getScheduledEntityUpdates()); $collections = array_merge($this->uow->getScheduledCollectionDeletions(), $this->uow->getScheduledCollectionUpdates()); /** @var PersistentCollection $collectionToChange */ foreach ($collections as $collectionToChange) { $entities = array_merge($entities, $collectionToChange->unwrap()->toArray()); } return array_filter($entities, function ($entity) { return $entity instanceof CustomerIdentityInterface && array_key_exists(ClassUtils::getClass($entity), $this->customerIdentities); }); }
/** * @internal * @return bool * @author Andreas Glaser */ protected function executeEvents() { $reRun = false; foreach ($this->unitOfWork->getScheduledEntityInsertions() as $hash => $entity) { if (array_key_exists($hash, $this->processedEntities['persist'])) { continue; } $this->initPersist($entity, true); $this->processRecalculationQueue(); $reRun = true; } foreach ($this->unitOfWork->getScheduledEntityUpdates() as $hash => $entity) { if (array_key_exists($hash, $this->processedEntities['update']) || array_key_exists($hash, $this->processedEntities['remove'])) { continue; } $this->initUpdate($entity, true); $this->processRecalculationQueue(); $reRun = true; } foreach ($this->unitOfWork->getScheduledEntityDeletions() as $hash => $entity) { if (array_key_exists($hash, $this->processedEntities['remove'])) { continue; } $this->initRemove($entity, true); $this->processRecalculationQueue(); $reRun = true; } return $reRun; }
/** * @param UnitOfWork $uow * @return array|Order[] */ protected function getChangedOrders(UnitOfWork $uow) { $entities = array_merge($uow->getScheduledEntityInsertions(), $uow->getScheduledEntityDeletions(), $uow->getScheduledEntityUpdates()); $collections = array_merge($uow->getScheduledCollectionDeletions(), $uow->getScheduledCollectionUpdates()); /** @var PersistentCollection $collectionToChange */ foreach ($collections as $collectionToChange) { $entities = array_merge($entities, $collectionToChange->unwrap()->toArray()); } return array_filter($entities, function ($entity) { return $this->isOrderValid($entity); }); }
/** * Main method: get all entities scheduled to be inserted, updated or deleted, * then remove duplicates and call setTags() method * * @param OnFlushEventArgs $args */ public function onFlush(OnFlushEventArgs $args) { $this->em = $args->getEntityManager(); $this->uow = $this->em->getUnitOfWork(); $entities = $this->uow->getScheduledEntityInsertions(); foreach ($this->uow->getScheduledEntityUpdates() as $key => $entity) { if (!in_array($entity, $entities)) { $entities[$key] = $entity; } } foreach ($entities as $entity) { if ($entity instanceof TaggableInterface) { $this->setTags($entity, true); } } if ($this->purge) { foreach ($this->uow->getScheduledEntityDeletions() as $key => $entity) { if ($entity instanceof TaggableInterface) { $this->purgeTags($entity); } } } }
public function onFlush(OnFlushEventArgs $eventArgs) { $this->em = $eventArgs->getEntityManager(); $this->conn = $this->em->getConnection(); $this->uow = $this->em->getUnitOfWork(); $this->platform = $this->conn->getDatabasePlatform(); $this->revisionId = null; // reset revision foreach ($this->uow->getScheduledEntityDeletions() as $entity) { $class = $this->em->getClassMetadata(get_class($entity)); if (!$this->metadataFactory->isAudited($class->name)) { continue; } $entityData = array_merge($this->getOriginalEntityData($entity), $this->uow->getEntityIdentifier($entity)); $this->saveRevisionEntityData($class, $entityData, 'DEL'); } }
public function onFlush(OnFlushEventArgs $eventArgs) { $this->em = $eventArgs->getEntityManager(); $this->quoteStrategy = $this->em->getConfiguration()->getQuoteStrategy(); $this->conn = $this->em->getConnection(); $this->uow = $this->em->getUnitOfWork(); $this->platform = $this->conn->getDatabasePlatform(); $this->revisionId = null; // reset revision $processedEntities = array(); foreach ($this->uow->getScheduledEntityDeletions() as $entity) { //doctrine is fine deleting elements multiple times. We are not. $hash = $this->getHash($entity); if (in_array($hash, $processedEntities)) { continue; } $processedEntities[] = $hash; $class = $this->em->getClassMetadata(get_class($entity)); if (!$this->metadataFactory->isAudited($class->name)) { continue; } $entityData = array_merge($this->getOriginalEntityData($entity), $this->uow->getEntityIdentifier($entity)); $this->saveRevisionEntityData($class, $entityData, 'DEL'); } foreach ($this->uow->getScheduledEntityInsertions() as $entity) { if (!$this->metadataFactory->isAudited(get_class($entity))) { continue; } $this->extraUpdates[spl_object_hash($entity)] = $entity; } foreach ($this->uow->getScheduledEntityUpdates() as $entity) { if (!$this->metadataFactory->isAudited(get_class($entity))) { continue; } $this->extraUpdates[spl_object_hash($entity)] = $entity; } }
/** * @param callable $callback Callback * @param string $entityClass Entity class filter * * @return AbstractOnFlushListener */ protected function onDelete(callable $callback, $entityClass = null) { $this->checkIfInitialized(); return $this->processEntities($this->uow->getScheduledEntityDeletions(), self::OPERATION_DELETE, $callback, $entityClass); }
/** * Get the scheduled object deletions from a UnitOfWork. * * @param \Doctrine\ORM\UnitOfWork $uow * @return array */ public function getScheduledObjectDeletions(UnitOfWork $uow) { return $uow->getScheduledEntityDeletions(); }
/** * Creates data * * @param UnitOfWork $uow * @return array */ public function createEmailAddressData(UnitOfWork $uow) { return ['updates' => array_map(function ($entity) use($uow) { return ['entity' => $entity, 'changeSet' => $uow->getEntityChangeSet($entity)]; }, array_filter(array_merge($uow->getScheduledEntityInsertions(), $uow->getScheduledEntityUpdates()), $this->getEntityFilter())), 'deletions' => array_filter($uow->getScheduledEntityDeletions(), $this->getEntityFilter())]; }
public function onFlush(OnFlushEventArgs $eventArgs) { if (!$this->active) { return; } // Clear updateData $this->updateData = $this->extraUpdates = array(); $this->em = $eventArgs->getEntityManager(); $this->conn = $this->em->getConnection(); $this->uow = $this->em->getUnitOfWork(); $this->platform = $this->conn->getDatabasePlatform(); $this->revisionId = null; // reset revision $this->draft = false; $processedEntities = array(); foreach ($this->uow->getScheduledEntityDeletions() as $entity) { if (!$this->annotationReader->isRevised(get_class($entity), true)) { continue; } //doctrine is fine deleting elements multiple times. We are not. $hash = $this->getHash($entity); if (in_array($hash, $processedEntities)) { continue; } $processedEntities[] = $hash; $this->extraUpdates[spl_object_hash($entity)] = $entity; $persister = $this->uow->getEntityPersister(get_class($entity)); $this->updateData[spl_object_hash($entity)] = $this->prepareUpdateData($persister, $entity); $entityData = array_merge($this->getOriginalEntityData($entity), $this->uow->getEntityIdentifier($entity)); $this->saveRevisionEntityData($this->em->getClassMetadata(get_class($entity)), $entityData, 'DEL'); if ($this->annotationReader->isDraft($entity) && $entity->isDraft()) { $this->resetRevisedData($entity); $this->setRevisionInfo($entity); $persister = $this->uow->getEntityPersister(get_class($entity)); $this->updateData[spl_object_hash($entity)] = $this->prepareUpdateData($persister, $entity); $fieldName = 'deletedAt'; $reflProp = new \ReflectionProperty($entity, $fieldName); $reflProp->setAccessible(true); $oldValue = $reflProp->getValue($entity); $reflProp->setValue($entity, null); $this->uow->scheduleExtraUpdate($entity, array($fieldName => array($oldValue, null))); } if (isset($this->softDeletes[spl_object_hash($entity)])) { $this->em->persist($entity); } } foreach ($this->uow->getScheduledEntityInsertions() as $entity) { if (!$this->annotationReader->isRevised(get_class($entity), true)) { continue; } $this->setRevisionInfo($entity); $this->extraUpdates[spl_object_hash($entity)] = $entity; $persister = $this->uow->getEntityPersister(get_class($entity)); $this->updateData[spl_object_hash($entity)] = $this->prepareUpdateData($persister, $entity); if ($this->annotationReader->isDraft($entity) && $entity->isDraft()) { $this->insertDrafts[spl_object_hash($entity)] = $entity; $this->resetRevisedData($entity); $this->uow->recomputeSingleEntityChangeSet($this->em->getClassMetadata(get_class($entity)), $entity); } } foreach ($this->uow->getScheduledEntityUpdates() as $entity) { if (!$this->annotationReader->isRevised(get_class($entity), true)) { continue; } $this->setRevisionInfo($entity); $this->extraUpdates[spl_object_hash($entity)] = $entity; $persister = $this->uow->getEntityPersister(get_class($entity)); $this->updateData[spl_object_hash($entity)] = $this->prepareUpdateData($persister, $entity); if ($this->annotationReader->isDraft($entity) && $entity->isDraft()) { $this->resetRevisedData($entity); } } }