Computes all the changes that have been done to entities and collections
since the last commit and stores these changes in the _entityChangeSet map
temporarily for access by the persisters, until the UoW commit is finished.
public computeChangeSets ( ) |
/** * @param \Doctrine\ORM\Event\PreFlushEventArgs $eventArgs * * @author Andreas Glaser */ public function preFlush(PreFlushEventArgs $eventArgs) { $this->entityManager = $eventArgs->getEntityManager(); $this->unitOfWork = $this->entityManager->getUnitOfWork(); if ($this->config['common_entity_event_handler']) { if (class_exists($this->config['common_entity_event_handler'])) { $this->commonEntityEventHandler = new $this->config['common_entity_event_handler']($this->container, $this, $this->entityManager); } else { throw new \InvalidArgumentException(strtr('":class" does not exist', [':class' => $this->config['common_entity_event_handler']])); } } $this->unitOfWork->computeChangeSets(); $max = 50; $current = 0; do { $runAgain = $this->executeEvents(); $current++; } while ($runAgain === true && $current <= $max); if ($current >= $max) { throw new \RuntimeException('Too many iterations... something must have gone wrong.'); } }
/** * Do the stuff * * @param TaggableInterface $entity * @param boolean $update true if entity is being updated, false otherwise */ protected function setTags(TaggableInterface $entity, $update = false) { $tagNames = $entity->getTagNames(); if (empty($tagNames)) { return; } // need to clone here, to avoid getting new tags $oldTags = is_object($entityTags = $entity->getTags()) ? clone $entityTags : $entityTags; $tagClassMetadata = $this->em->getClassMetadata(get_class($this->tag)); $repository = $this->em->getRepository(get_class($this->tag)); foreach ($tagNames as $tagName) { $tag = $repository->findOneByName($tagName); if (empty($tag)) { // if tag doesn't exist, create it $tag = clone $this->tag; $tag->setName($tagName); $this->em->persist($tag); // see http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#onflush $this->uow->computeChangeSet($tagClassMetadata, $tag); } if (!$entity->hasTag($tag)) { // add tag only if not already added $entity->addTag($tag); } } // if updating, need to check if some tags were removed if ($update) { foreach ($oldTags as $oldTag) { if (!in_array($oldTag->getName(), $tagNames)) { $entity->removeTag($oldTag); } } } // see http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#onflush $entityClassMetadata = $this->em->getClassMetadata(get_class($entity)); $this->uow->computeChangeSets($entityClassMetadata, $entity); }
public function onFlush(OnFlushEventArgs $e) { $this->init($e); if ($this->stats) { foreach ($this->uow->getScheduledEntityInsertions() as $entity) { if ($entity instanceof Deposit) { // deposit arrived if ($entity->isConfirmed()) { // it is confirmed $this->addConfirmedDeposit($entity); } else { // it is pending $entity->getAccount()->addTotalPendingDeposit($entity->getAmount()); $this->stats->addTotalPendingDeposit($entity->getAmount()); } $this->stats->setLastDeposit($entity->getReceivedTime()); $this->recoumputeChanges($entity->getAccount()); } if ($entity instanceof PayoutTx) { // transaction has just been sent $this->stats->addTotalTxFees($entity->getTxFee() * -1); } } foreach ($this->uow->getScheduledEntityUpdates() as $entity) { if ($entity instanceof Deposit) { $changes = $this->uow->getEntityChangeSet($entity); if (isset($changes['confirmed'])) { $oldConfirmed = $changes['confirmed'][0]; $newConfirmed = $changes['confirmed'][1]; if (!$oldConfirmed && $newConfirmed) { // subtract pending if not referrer $this->stats->subtractTotalPendingDeposit($entity->getAmount()); $entity->getAccount()->subtractTotalPendingDeposit($entity->getAmount()); // add confirmed $this->addConfirmedDeposit($entity); $this->recoumputeChanges($entity->getAccount()); } } } if ($entity instanceof Payout) { $changes = $this->uow->getEntityChangeSet($entity); if (isset($changes['paid'])) { $oldPaid = $changes['paid'][0]; $newPaid = $changes['paid'][1]; if (!$oldPaid && $newPaid) { if ($entity->isDefaultPayout() || $entity->isRemainingFundsReturn() || $entity->isLastPayout()) { $this->stats->addTotalPayout($entity->getAmount()); $entity->getAccount()->addTotalPayout($entity->getAmount()); if ($entity->isDefaultPayout()) { $this->stats->setLastPayout($entity->getPaidOutTime()); } } elseif ($entity->isReferrerPayout()) { $this->stats->addTotalReferralPayout($entity->getAmount()); $entity->getAccount()->addTotalReferralPayout($entity->getAmount()); } if (!$entity->isLastAdminPayout()) { $this->recoumputeChanges($entity->getAccount()); } $this->stats->addTotalFees($entity->getFee()); } } } } $this->recoumputeChanges($this->stats); $this->uow->computeChangeSets(); } }
/** * @param object $entity entity * @param ObjectManager $entityManager entityManager * @param UnitOfWork $uow uow */ private function computeViolations($entity, ObjectManager $entityManager, UnitOfWork $uow) { if (!($handlers = $this->handlerManager->fetch($entity))) { return null; } $list = $this->violationManager->getViolationListNotFixed($entity); $list->setFixed(true); foreach ($handlers as $handler) { $handler->validate($entity, $list); } $uow->computeChangeSets(); foreach ($list as $violation) { $changeSet = $uow->getEntityChangeSet($violation); if (!empty($changeSet) || !$entityManager->contains($violation)) { $this->violations[spl_object_hash($violation)] = $violation; } } }