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(); }
public function invalidate() { $ormConfig = $this->entityManager->getConfiguration(); $dbalConfig = $this->entityManager->getConnection()->getConfiguration(); $cache = array($ormConfig->getHydrationCacheImpl(), $ormConfig->getMetadataCacheImpl(), $ormConfig->getQueryCacheImpl(), $ormConfig->getResultCacheImpl(), $dbalConfig->getResultCacheImpl()); foreach ($cache as $impl) { if (!$impl instanceof CacheProvider) { continue; } $impl->deleteAll(); } }
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(); } }
public function invalidate() { $ormConfig = $this->entityManager->getConfiguration(); $dbalConfig = $this->entityManager->getConnection()->getConfiguration(); $cache = $this->cacheStorages; $cache[] = $ormConfig->getHydrationCacheImpl(); $cache[] = $ormConfig->getMetadataCacheImpl(); $cache[] = $ormConfig->getQueryCacheImpl(); $cache[] = $ormConfig->getResultCacheImpl(); $cache[] = $dbalConfig->getResultCacheImpl(); foreach ($cache as $impl) { if (!$impl instanceof ClearableCache) { continue; } $impl->deleteAll(); } }
/** * @param EntityManager $em */ public function __construct(EntityManager $em) { $this->em = $em; $this->db = $em->getConnection(); $this->platform = $this->db->getDatabasePlatform(); $this->quotes = $em->getConfiguration()->getQuoteStrategy(); $this->uow = $this->em->getUnitOfWork(); }
/** * @return IWrappedConnection */ private function getWrappedPDOConnection() { $wrappedConnection = $this->entityManager->getConnection()->getWrappedConnection(); if (!$wrappedConnection instanceof IWrappedConnection) { $message = sprintf('Change "preventImplicitFlush" to FALSE or set doctrine connection "driverClass" to %s.', MySqlDriver::class); throw new LogicException($message); } return $wrappedConnection; }
/** * @param Locality $locality * @return Locality * @throws \Doctrine\DBAL\DBALException * @throws \Exception */ public function setupLocalityEntity(Locality $locality) { /* In order to NOT auto increment locality ID counter in DB by INSERTs that actually wont happen (e.g. safePersist()) and because Doctrine2 does NOT support locking of entire tables, we have to use native SQL(MySQL) query. DUAL is "dummy" table - there is no need to reference any table (more info in MySQL SELECT documentation) */ $this->em->getConnection()->executeQuery('INSERT INTO locality (name) SELECT :name FROM DUAL WHERE NOT EXISTS( SELECT l.name FROM locality l WHERE l.name = :name) LIMIT 1', ['name' => $locality->getName()]); $result = $this->em->createQuery('SELECT l FROM ' . Locality::class . ' l WHERE l.name = :name')->setParameters(['name' => $locality->getName()])->getSingleResult(); return $result; }
/** * @param WorkedHours $workedHours * @return WorkedHours * @throws \Doctrine\DBAL\DBALException * @throws \Exception */ public function setupWorkedHoursEntity(WorkedHours $workedHours) { $values = $workedHours->toArray(true); /* In order to NOT auto increment workedHours ID counter in DB by INSERTs that actually wont happen (e.g. safePersist()) and because Doctrine2 does NOT support locking of entire tables, we have to use native SQL(MySQL) query. DUAL is "dummy" table - there is no need to reference any table (more info in MySQL SELECT documentation) */ $this->em->getConnection()->executeQuery('INSERT INTO worked_hours (work_start, work_end, lunch, other_hours) SELECT :workStart, :workEnd, :lunch, :otherHours FROM DUAL WHERE NOT EXISTS( SELECT work_start, work_end, lunch, other_hours FROM worked_hours WHERE work_start = :workStart AND work_end = :workEnd AND lunch = :lunch AND other_hours = :otherHours) LIMIT 1', $values); $result = $this->em->createQuery('SELECT wh FROM ' . WorkedHours::class . ' wh WHERE wh.workStart = :workStart AND wh.workEnd = :workEnd AND wh.lunch = :lunch AND wh.otherHours = :otherHours')->setParameters($values)->getOneOrNullResult(); return $result; }