Doctrine ORM: drop all tables without dropping database

Viewed 39366

I have to work with an existing database (not managed with Doctrine) and I want to use doctrine only for new tables in this db.

is there a way to tell Doctrine to not drop the entire DB on reload but only the models defined in the yaml file ?

8 Answers

I this Symfony command to drop all tables:

bin/console doctrine:schema:drop --full-database --force

I added flag --force after get a caution message.

For Symfony 3:

$entityManager = $container->get('doctrine.orm.entity_manager'); 

$entityManager->getConnection()->getConfiguration()->setSQLLogger(null);

$entityManager->getConnection()->prepare("SET FOREIGN_KEY_CHECKS = 0;")->execute();

foreach ($entityManager->getConnection()->getSchemaManager()->listTableNames() as $tableNames) {
        $sql = 'DROP TABLE ' . $tableNames;
        $entityManager->getConnection()->prepare($sql)->execute();
}
$entityManager->getConnection()->prepare("SET FOREIGN_KEY_CHECKS = 1;")->execute();

Old question, but adding for a future soul.

Based on Meezaan-ud-Din's answer quickly found it for Zend Framework 3 + Doctrine 2

./vendor/bin/doctrine-module orm:schema-tool:drop --full-database -f --dump-sql

Do NOT use in production

  • orm:schema-tool:drop to "drop" database
  • --full-database to wipe out everything in database which is managed by Doctrine!
  • To execute, you must use --force (or -f)
  • --dump-sql to show the SQL being executed. May be combined with the -f flag.

Complete code for execution found in class: \Doctrine\ORM\Tools\Console\Command\SchemaTool\DropCommand

To run the command bin/console doctrine:schema:drop --force from inside another command, try this:

$command = $this->getApplication()->find('doctrine:schema:drop');
$returnCode = $command->run(new ArrayInput(['--force' => true]), $output);
Related