/** * @param Listing $listing * @param bool $withItems * @param array|null $valuesForNewListing * @return Listing * @throws \Exception */ public function establishListingCopy(Listing $listing, $withItems = true, array $valuesForNewListing = null) { Validators::assert($withItems, 'boolean'); $newListing = clone $listing; if (isset($valuesForNewListing)) { $newListing->setDescription($valuesForNewListing['description']); $newListing->setHourlyWage($valuesForNewListing['hourlyWage']); } $this->em->persist($newListing); if ($withItems === true) { $copies = $this->getItemsCopies($listing); if (!empty($copies)) { foreach ($copies as $copy) { $copy->setListing($newListing); $this->em->persist($copy); } } } try { $this->em->flush(); $this->em->clear(); } catch (\Exception $e) { $this->onError('establishListingCopy', $e, self::class); throw $e; } return $newListing; }
public function clearDatabase() { $connection = $this->em->getConnection(); $tables = $connection->getSchemaManager()->listTableNames(); $connection->prepare('SET FOREIGN_KEY_CHECKS = 0')->execute(); foreach ($tables as $table) { if ($table !== 'db_version') { $connection->prepare('TRUNCATE TABLE `' . $table . '`')->execute(); } } $connection->prepare('SET FOREIGN_KEY_CHECKS = 1')->execute(); $this->em->clear(); }
private function stopTransactionWrapper() { if (!$this->isInitialized) { return; } if ($this->isImplicitFlushPrevented) { $wrappedConnection = $this->getWrappedPDOConnection(); $wrappedConnection->unwrap(); } /** @var IWrappedConnection|Connection $connection */ $connection = $this->entityManager->getConnection(); $connection->unwrap(); $this->isInitialized = FALSE; $this->entityManager->clear(); }
public function prepareDatabaseTest() { $this->lock(); $this->em->clear(); $migrationConfig = $this->context->getByType(Configuration::class); /** @var Configuration $migrationConfig */ $migration = new Migration($migrationConfig); try { $migration->migrate(); } catch (MigrationException $ex) { if ($ex->getCode() !== 4) { // no migrations found; this should not break tests in early stages of development, // the tests will fail when they require a model anyway throw $ex; } } $this->clearDatabase(); }
protected function createAndUpdate(ORMMetadata $class, EntityRepository $repository) { $paginator = new Nette\Utils\Paginator(); $paginator->itemsPerPage = 1000; $countQuery = $this->buildCountForUpdateQuery($repository)->getQuery(); $paginator->itemCount = $countQuery->getSingleScalarResult(); $this->onIndexStart($this, $paginator, $class); if ($paginator->itemCount <= 0) { return; } $qb = $this->buildSelectForUpdateQuery($repository, $class); if ($identifier = $class->getSingleIdentifierColumnName()) { $qb->orderBy(sprintf('e.%s', $identifier), 'ASC'); } $lastId = NULL; $selectQueryBuilder = $qb->setMaxResults($paginator->getLength()); while (1) { if ($lastId !== NULL) { $qbCopy = clone $selectQueryBuilder; $query = $qbCopy->andWhere(sprintf('e.%s > :lastId', $identifier))->setParameter('lastId', $lastId)->getQuery(); } else { $query = $selectQueryBuilder->getQuery(); if (!$identifier) { $query->setFirstResult($paginator->getOffset()); } } $beginTime = microtime(TRUE); $entities = $query->getResult(); $this->postFetch($entities, $repository, $class); $loadedTime = microtime(TRUE); $this->doPersistEntities($entities); $this->searchManager->flush(); $this->searchManager->clear(); try { $this->onItemsIndexed($this, $entities); } catch (\Exception $e) { } if ($identifier) { $lastIdentifier = $class->getIdentifierValues(end($entities)); // [id => value] $lastId = reset($lastIdentifier); if (!is_numeric($lastId)) { trigger_error(E_USER_WARNING, 'Expected numeric identifier'); } } $this->entityManager->clear(); $this->onIndexStats($this, $class, microtime(TRUE) - $loadedTime, $loadedTime - $beginTime); if ($paginator->isLast()) { break; } $paginator->page += 1; } }
public function process(AMQPMessage $message) : int { try { if (!($request = Json::decode($message->body, Json::FORCE_ARRAY))) { return self::MSG_REJECT; } } catch (JsonException $e) { $this->logger->addError($e->getMessage()); return self::MSG_REJECT; } $orderId = $request['orderId']; try { $conn = $this->em->getConnection(); if ($conn->ping() === false) { $conn->close(); $conn->connect(); } /** @var Order $order */ $orders = $this->em->getRepository(Order::class); if (!($order = $orders->find($orderId))) { $this->logger->addWarning(sprintf('Order %s not found in db', $orderId)); return self::MSG_REJECT; } if (!$order->getUser()) { $this->logger->addWarning(sprintf('Order %s has no user', $orderId)); return self::MSG_REJECT; } $this->user->passwordLessLogin($order->getUser()->getId()); return $this->processOrder($order, $request['options']); } catch (\App\Exception\DatabaseDeadlockException $e) { return $this->restartJob($request, $e); } catch (\Doctrine\DBAL\Exception\DriverException $e) { if ($deadlock = \App\Exception\DatabaseDeadlockException::fromDriverException($e)) { $e = $deadlock; $message = null; } else { $this->logger->addError($e->getMessage(), ['exception' => $e]); $message = $e->getPrevious() instanceof \Doctrine\DBAL\Driver\PDOException ? $e->getPrevious()->getMessage() : $e->getMessage(); } return $this->restartJob($request, $e, $message); } catch (\Exception $e) { $this->logger->addError($this->getQueue(), ['exception' => $e]); return $this->restartJob($request, $e); } finally { $this->user->logout(true); $this->em->clear(); } }
/** * * @param integer $flag IConsumer constant * @return integer IConsumer constant */ private function processEnd($flag) { $status = RmqLogConsumer::STATUS_OK; try { $this->em->clear(); } catch (\Exception $e) { $status = RmqLogConsumer::STATUS_ERROR; // Fatal error during em->clear(); } try { return $this->processTermination($flag, $status); } catch (\Exception $e) { // Fatal error during termination } return $flag; }