Doctrine ORM: drop all tables without dropping dat

2020-06-08 15:03发布

问题:

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 ?

回答1:

I know this a very old question, and it appears you are using symfony.

Try:

app/console --env=prod doctrine:schema:drop --full-database

This will drop all the tables in the DB.



回答2:

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();


回答3:

My 2 cents, if you want do this via Php:

    $em = ....getContainer()->get('doctrine')->getManager();

    $metaData = $em->getMetadataFactory()->getAllMetadata();

    $tool = new \Doctrine\ORM\Tools\SchemaTool($em);
    $tool->dropSchema($metaData);
    $tool->createSchema($metaData);

This is a bit slow, you can also delete all data from all tables:

$em = getContainer()->get('doctrine.dbal.default_connection');

foreach($em->getSchemaManager()->listTableNames() as $tableName)
{
  $em->exec('DELETE FROM ' . $tableName);
}


回答4:

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



回答5:

You can find a task I use to truncate all tables from a db at this gist: https://gist.github.com/1154458

The core code is:

        $this->dbh = $connection->getDbh();
        $this->dbh->query(sprintf('SET FOREIGN_KEY_CHECKS = 0;'));
        $tables = $connection->import->listTables();
        foreach ($tables as $table)
        {
          $this->dbh->query(sprintf('TRUNCATE TABLE %s', $tableName));
        }
        $this->dbh->query(sprintf('SET FOREIGN_KEY_CHECKS = 1;'));


回答6:

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.



回答7:

Yes, surely you can use doctrine only for some tables(not all). And it won't drop all the other tables., unless you manually run

$manager->dropDatabases();

This is how you can start using Doctine for your new tables:

  1. Setup DB Connection

  2. Setup Doctrine's files/directory structure

  3. Use Command Line Interface in order to automatically generate all Doctrine models(and schema) based on DB tables(you can delete unncessesary models manually).



回答8:

@ gpilotino Yes, I'm having a similar problem. There seems to be NO WAY to drop and rebuild the database from within PHPUnit, (The future of Symfony testing).

Maybe it's possible in 'lime',I don't know.

So, I am having to write a reverse ->save() function that backs all the data out of the database, and then resets all the sequences so that I can do automated testing.

For those who don't want to follow in my frustration I tried both:

1) using a Task from inside of symfony:

  $optionsArray=array();
  $argumentsArray=array();

  $optionsArray[]="--all";
  $optionsArray[]="--and-load";
  $optionsArray[]="--no-confirmation";

  $task = new sfDoctrineBuildTask($configuration->getEventDispatcher(), new sfFormatter());
  $task->run($argumentsArray, $optionsArray);

2)Executing it from outside of symfony while inside of PHP:

  Doctrine_Manager::getInstance()->getCurrentConnection()->close();
  exec('./symfony doctrine:build --all --and-load --no-confirmation');

The reason that I closed the connection is that Postgres, MDBOC (my db of choice) will not drop a database that has a connection. Probably is STILL some kind of problem. I tell ya, it's NEVER as easy as the simple tutorials show. And it's even WORSE with microslop products.



标签: orm doctrine