private function cleanDB() { $this->mainlogrepo->getLoggableListener()->setEnabled(false); $this->doctrine->getConnection()->executeQuery('SET foreign_key_checks = 0'); $this->doctrine->getManager()->getFilters()->disable('softdeleteable'); foreach ($this->mainlogrepo->findAll() as $result) { $this->doctrine->getManager()->remove($result); } $this->doctrine->getManager()->flush(); $fixturesClasses = array(get_class(new Comment()), get_class(new RelatedArticle()), get_class(new User()), get_class(new Article())); foreach ($fixturesClasses as $fixturesClass) { $manager = $this->doctrine->getManagerForClass($fixturesClass); $repo = $manager->getRepository($fixturesClass); foreach ($repo->findAll() as $result) { $manager->remove($result); } $manager->flush(); $manager->clear(); } //make twice to be sure ??? foreach ($fixturesClasses as $fixturesClass) { $manager = $this->doctrine->getManagerForClass($fixturesClass); $repo = $manager->getRepository($fixturesClass); foreach ($repo->findAll() as $result) { $manager->remove($result); } $manager->flush(); $manager->clear(); } $this->doctrine->getManager()->getFilters()->enable('softdeleteable'); $this->doctrine->getConnection()->executeQuery('SET foreign_key_checks = 1'); $this->mainlogrepo->getLoggableListener()->setEnabled(true); }
protected function testDatabase() { try { $this->em->getConnection()->connect(); } catch (\Exception $e) { $this->logger->error('No database connection.'); exit; } }
public function execute() { $connection = $this->em->getConnection(); $statement = $connection->prepare('SELECT * FROM campaignchain_campaign_repeating_instance'); $statement->execute(); $instances = $statement->fetchAll(); try { $this->em->getConnection()->beginTransaction(); foreach ($instances as $instance) { /** @var Campaign $campaignParent */ $campaignParent = $this->em->getRepository('CampaignChainCoreBundle:Campaign')->find($instance['repeatingCampaign_id']); /** @var Campaign $campaignChild */ $campaignChild = $this->em->getRepository('CampaignChainCoreBundle:Campaign')->find($instance['scheduledCampaign_id']); $campaignParent->addChild($campaignChild); $campaignChild->setParent($campaignParent); $this->em->flush(); } $this->em->getConnection()->commit(); } catch (\Exception $e) { $this->em->getConnection()->rollback(); throw $e; } }
/** * @param array $files * @param bool $doDrop */ public function load(array $files, $doDrop = true) { try { $this->em->getConnection()->beginTransaction(); $userProcessor = new UserProcessor(realpath(SystemUtil::getRootDir() . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR), $this->userService, $this->mimeTypeGuesser, $this->extensionGuesser); // Create Alice manager and fixture set $this->fixtureManager->addProcessor($userProcessor); $set = $this->fixtureManager->createFixtureSet(); // Add the fixture files foreach ($files as $file) { $set->addFile($file, 'yaml'); } $set->setDoDrop($doDrop); $set->setDoPersist(true); $set->setSeed(1337 + 42); // TODO Keep Module data intact $bundles = $this->em->getRepository("CampaignChain\\CoreBundle\\Entity\\Bundle")->findAll(); $modules = $this->em->getRepository("CampaignChain\\CoreBundle\\Entity\\Module")->findAll(); if ($this->fixtureManager->load($set)) { // TODO: Restore modules data foreach ($bundles as $bundle) { $this->em->persist($bundle); } foreach ($modules as $module) { $this->em->persist($module); } $this->em->flush(); $this->em->getConnection()->commit(); return true; } return false; } catch (\Exception $e) { $this->em->getConnection()->rollback(); $this->setException($e); return false; } }
class Registry { /** * Returns the connection */ public static function getConnection() { return new Connection(array("host" => "localhost", "username" => "root", "password" => "secret", "dbname" => "invo")); } } class SomeComponent { protected $_connection; /** * Sets the connection externally */ public function setConnection($connection) { $this->_connection = $connection; } public function someDbTask() { $connection = $this->_connection; // ... } } $some = new SomeComponent(); //Pass the connection defined in the registry $some->setConnection(Registry::getConnection()); $some->someDbTask();
{ protected $_connection; /** * Sets the connection externally */ public function setConnection($connection) { $this->_connection = $connection; } /** * This method always needs the shared connection */ public function someDbTask() { $connection = $this->_connection; // ... } /** * This method always needs a new connection */ public function someOtherDbTask($connection) { } } $some = new SomeComponent(); //This injects the shared connection $some->setConnection(Registry::getSharedConnection()); $some->someDbTask(); //Here, we always pass a new connection as parameter $some->someOtherDbTask(Registry::getConnection());
/** * @param SymfonyStyle|null $io * @param bool $updateDatabase * * @return bool * * @throws \Doctrine\DBAL\ConnectionException * @throws \Exception */ public function install(SymfonyStyle $io = null) { $this->logger->info('START: MODULES INSTALLER'); $newBundles = $this->bundleConfigService->getNewBundles(); if (empty($newBundles)) { if ($io) { $io->success('No new modules found.'); } $this->logger->info('No new modules found.'); $this->logger->info('END: MODULES INSTALLER'); return false; } // Increase timeout limit to run this script. set_time_limit(240); $this->kernelService->parseBundlesForKernelConfig($newBundles); $loggerResult = ''; $this->em->getConnection()->beginTransaction(); $this->registerDistribution(); try { foreach ($newBundles as $newBundle) { switch ($newBundle->getType()) { case 'campaignchain-core': break; case 'campaignchain-hook': // TODO: new vs. update $this->registerHook($newBundle); break; case 'campaignchain-symfony': $this->registerSymfonyBundle($newBundle); break; default: $this->registerModule($newBundle); break; } $loggerResult .= $newBundle->getName() . ', '; $this->em->persist($newBundle); } $this->em->flush(); // Store the campaign types a campaign can be copied to. $this->registerCampaignConversions(); // Store the channels related to an activity or Location. $this->registerChannelRelationships(); // Store the system parameters injected by modules. $this->registerModuleSystemParams(); // Register any new Bundles in the Symfony kernel. $this->kernelService->register(); $this->em->getConnection()->commit(); } catch (\Exception $e) { $this->em->getConnection()->rollBack(); if ($io) { $io->error('Error at update: ' . $e->getMessage()); } $this->logger->error('Error: ' . $e->getMessage()); $this->logger->info('END: MODULES INSTALLER'); throw $e; } $this->logger->info('Installed/updated modules: ' . rtrim($loggerResult, ', ')); if ($io) { $io->section('Installed/updated modules:'); $io->listing(explode(', ', rtrim($loggerResult, ', '))); } $this->logger->info('END: MODULES INSTALLER'); return true; }