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 ?
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 ?
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.
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();
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);
}
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!--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
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;'));
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.
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:
Setup DB Connection
Setup Doctrine's files/directory structure
Use Command Line Interface in order to automatically generate all Doctrine models(and schema) based on DB tables(you can delete unncessesary models manually).
@ 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.