/** * Initialization of data required to run current command * @param InputInterface $input * @param OutputInterface $output */ protected function initialize(InputInterface $input, OutputInterface $output) { $this->kernelDir = $this->getContainer()->getParameter('kernel.root_dir'); $this->entityManager = $this->getContainer()->get('doctrine.orm.entity_manager'); $this->connection = $this->entityManager->getConnection(); $this->dbPlatform = $this->connection->getDatabasePlatform(); $this->output = $output; }
/** * Perform the actual purge by truncating each of tables from the provided list * Throws an Exception (in case if anything goes wrong) which stops the process of truncating to prevent * the possible damage to DB * * @param array $tablesList * @throws \Exception */ protected function purgeTables(array $tablesList) { $connection = $this->entityManager->getConnection(); $dbPlatform = $connection->getDatabasePlatform(); $connection->query('SET FOREIGN_KEY_CHECKS=0;'); foreach ($tablesList as $table) { $query = $dbPlatform->getTruncateTableSql($table); try { $this->output->writeln('Purging data from ' . $table); $connection->executeUpdate($query); } catch (\Exception $e) { $this->output->writeln('Error purging data from \'' . $table . '\'. Error: ' . $e->getMessage()); $connection->query('SET FOREIGN_KEY_CHECKS=1;'); throw $e; } } $connection->query('SET FOREIGN_KEY_CHECKS=1;'); }