transactional() public method

The function gets passed this EntityManager instance as an (optional) parameter. {@link flush} is invoked prior to transaction commit. If an exception occurs during execution of the function or flushing or transaction commit, the transaction is rolled back, the EntityManager closed and the exception re-thrown.
public transactional ( Closur\Closure $func )
$func Closur\Closure The function to execute transactionally.
 /**
  * @param float $discount
  */
 public function apply($discount)
 {
     $this->entityManager->transactional(function () use($discount) {
         foreach ($this->items->findAll() as $item) {
             $item->applyDiscount($discount);
         }
     });
     $this->entityManager->clear();
 }
Ejemplo n.º 2
1
 public function deleteByKey($id)
 {
     $repo = $this->repository;
     $this->em->transactional(function (EntityManager $em) use($id, $repo) {
         $oneRecord = $repo->find($id);
         if ($oneRecord != null) {
             $em->remove($oneRecord);
         } else {
             throw new \Exception("Could not find matching record to delete");
         }
     });
 }
 /**
  * @param Doctrine\ORM\EntityManager $entityManager
  */
 private function _save(EntityManager $entityManager)
 {
     $entityManager->transactional(function ($em) {
         foreach ($this->queue as $entity) {
             $entityManager->persist($entity);
         }
     });
 }
Ejemplo n.º 4
0
 /**
  * вернуть деньги с баланса кампании на счёт юзера
  *
  * @param Campaign $campaign
  */
 public function transferMoneyBackToUser(Campaign $campaign)
 {
     $this->em->transactional(function (EntityManager $em) use($campaign) {
         $em->getRepository('VifeedUserBundle:User')->updateBalance($campaign->getUser(), $campaign->getBalance());
         $campaign->setBalance(0);
         $em->persist($campaign);
     });
 }
Ejemplo n.º 5
0
 /**
  * You may use an arbitrary SQL statement to receive Agency objects.
  * Your statement should at least return the id of the agencies.
  *
  * @param string $sql
  * @throws MissingAttributesDataBackendIOException
  * @throws DataBackendIOException
  * @throws DataBackendException
  * @return Agency[]
  */
 public function getAgencies($sql)
 {
     $agencies = array();
     $backend = $this;
     $em = $this->em;
     $this->em->transactional(function () use(&$agencies, $sql, $backend, $em) {
         $stmt = $em->getConnection()->executeQuery($sql);
         foreach ($stmt as $result) {
             if (!array_key_exists('id', $result)) {
                 throw new MissingAttributesDataBackendIOException();
             }
             $id = $result["id"];
             $agency = $em->find("malkusch\\bav\\Agency", $id);
             $agencies[] = $agency;
             $agency->getBank()->setDataBackend($backend);
         }
     });
     return $agencies;
 }
Ejemplo n.º 6
0
 /**
  *
  * @param int $id
  * @todo метод незащищен от CSRF атаки
  */
 public function getAndClickLinkById($id)
 {
     $user = $this->user->getUserObject();
     $bizAction = $this->em->getRepository('AppBundle:BizAction')->findActiveAction($id);
     $url = $bizAction->getUrl();
     $this->em->transactional(function ($em) use($user, $bizAction) {
         $f = $em->getRepository('AppBundle:UserClientActionsClicks')->findOneBy(['action' => $bizAction, 'user' => $user]);
         if (is_null($f)) {
             $f = new UserClientActionsClicks();
             $f->setUser($user);
             $f->setAction($bizAction);
             $f->setClickPrice($bizAction->getBonusClick());
             $f->clickPriceActual = true;
         } else {
             $f->incTotalCount();
         }
         $em->persist($f);
     });
     return $url;
 }
 /**
  * @see BackendAdapter::createFile
  */
 public function createFile(File $file, Folder $folder)
 {
     $self = $this;
     return $this->em->transactional(function (EntityManager $em) use($self, $file, $folder) {
         $fileEntityName = $self->getFileEntityName();
         $entity = new $fileEntityName();
         $entity->setFolder($self->getFolderReference($folder->getId()));
         $entity->setName($file->getName());
         $entity->setProfile($file->getProfile());
         $entity->setDateCreated($file->getDateCreated());
         $entity->setStatus($file->getStatus());
         $entity->setUuid($file->getUuid());
         $entity->setData($file->getData()->toArray());
         $resource = $file->getResource();
         if ($resource) {
             $entity->setResource($em->getReference($self->getResourceEntityName(), $resource->getId()));
         }
         $em->persist($entity);
         $em->flush($entity);
         $file->setId($entity->getId());
         $file->setFolderId($entity->getFolder()->getId());
         return $file;
     });
 }
Ejemplo n.º 8
0
 /**
  * Move the node to the new position and change level by {$levelDiff}
  * @param Node\DoctrineNode $node
  * @param int $pos
  * @param int $levelDiff
  */
 public function move(Node\NodeInterface $node, $pos, $levelDiff, $undoMove = false)
 {
     if (!$this->locked) {
         //\Log::info('Should lock before changes');
     }
     $tableName = $this->tableName;
     $arrayHelper = $this->arrayHelper;
     $self = $this;
     // Calculate the old position to rollback to in case of some issue
     $oldPosition = $node->getLeftValue();
     if ($pos < $oldPosition) {
         $oldPosition = $node->getRightValue() + 1;
     }
     if (!$node instanceof Node\DoctrineNode) {
         throw new Exception\WrongInstance($node, 'Node\\DoctrineNode');
     }
     // Transactional because need to rollback in case of trigger failure
     $this->entityManager->transactional(function (EntityManager $entityManager) use($node, $pos, $levelDiff, $tableName, $arrayHelper, $self) {
         $left = $node->getLeftValue();
         $right = $node->getRightValue();
         $spaceUsed = $right - $left + 1;
         $moveA = null;
         $moveB = null;
         $a = null;
         $b = null;
         $min = null;
         $max = null;
         if ($pos > $left) {
             $a = $right + 1;
             $b = $pos - 1;
             $moveA = $pos - $left - $spaceUsed;
             $moveB = -$spaceUsed;
             $min = $left;
             $max = $pos - 1;
         } else {
             $a = $pos;
             $b = $left - 1;
             $moveA = $pos - $left;
             $moveB = $spaceUsed;
             $min = $pos;
             $max = $right;
         }
         //			// NB! It's important to set "level" before "left" for MySQL!
         //			$dql = "UPDATE {$className} e
         //					SET e.level = e.level + IF(e.left BETWEEN {$left} AND {$right}, {$levelDiff}, 0),
         //						e.left = e.left + IF(e.left BETWEEN {$left} AND {$right}, {$moveA}, IF(e.left BETWEEN {$a} AND {$b}, {$moveB}, 0)),
         //						e.right = e.right + IF(e.right BETWEEN {$left} AND {$right}, {$moveA}, IF(e.right BETWEEN {$a} AND {$b}, {$moveB}, 0))
         //					WHERE (e.left BETWEEN {$min} AND {$max}
         //						OR e.right BETWEEN {$min} AND {$max})";
         //
         //			$dql .= $self->getAdditionalCondition('AND');
         //
         //			$query = $entityManager->createQuery($dql);
         //			$query->execute();
         $sql = "UPDATE {$tableName}\n\t\t\t\t\tSET lvl = lvl + IF(lft BETWEEN {$left} AND {$right}, {$levelDiff}, 0),\n\t\t\t\t\t\tlft = lft + IF(lft BETWEEN {$left} AND {$right}, {$moveA}, IF(lft BETWEEN {$a} AND {$b}, {$moveB}, 0)),\n\t\t\t\t\t\trgt = rgt + IF(rgt BETWEEN {$left} AND {$right}, {$moveA}, IF(rgt BETWEEN {$a} AND {$b}, {$moveB}, 0))\n\t\t\t\t\tWHERE (lft BETWEEN {$min} AND {$max}\n\t\t\t\t\t\tOR rgt BETWEEN {$min} AND {$max})";
         $sql .= $self->getAdditionalConditionSql('AND');
         $entityManager->getConnection()->exec($sql);
         // Change node parameters locally as well
         // TODO: how to rollback these changes if nested set post move trigger fails?
         $arrayHelper->move($node, $pos, $levelDiff);
     });
     // Trigger post move event. Only after transaction is commited because
     // public schema must update it's stuff as well
     try {
         $masterNode = $node->getMasterNode();
         $eventArgs = new Event\NestedSetEventArgs($masterNode, $this->entityManager);
         $this->entityManager->getEventManager()->dispatchEvent(Event\NestedSetEvents::nestedSetPostMove, $eventArgs);
     } catch (\Exception $e) {
         //TODO: new pages should be removed
         // Should not happen
         if ($undoMove) {
             throw $e;
         }
         // Undo move
         $this->move($node, $oldPosition, -$levelDiff, true);
         throw $e;
     }
 }
Ejemplo n.º 9
0
 public function executeAtomically(callable $operation)
 {
     return $this->entity_manager->transactional($operation);
 }
Ejemplo n.º 10
0
 /**
  * {@inheritDoc}
  *
  * @static 
  */
 public static function transactional($func)
 {
     return \Doctrine\ORM\EntityManager::transactional($func);
 }
 /**
  * {@inheritdoc}
  */
 public function transactional($func)
 {
     return $this->wrapped->transactional($func);
 }
Ejemplo n.º 12
0
 /**
  * Transactional Doctrine wrapper.
  *
  * Instead of the EntityManager, this inserts this Mapper into the
  * function.
  *
  * @param Closure $func
  */
 public function transactional(\Closure $func)
 {
     return $this->em->transactional(function ($em) use($func) {
         return $func($this);
     });
 }