The Problem
This is regarding symfony 2.1 CLI tasks. When trying to add a task that dropped, created, and updated my database, I received the following:
[Doctrine\DBAL\DBALException]
An exception occurred while executing 'SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'':
SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected
[PDOException]
SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected
The problem, it turns out, was that doctrine was using the same connection that it used to drop the database as it was to update the database. After the drop, doctrine lost it's awareness to your database and did not re-establish it upon create.
The Fix
You need to close the connection after calling doctrine:database:drop and before doctrine:database:update --force
$connection = $this->getApplication()->getKernel()->getContainer()->get('doctrine')->getConnection();
if ($connection->isConnected()) {
$connection->close();
}
And just for reference:
Drop, Create, and Update database in Symfony 2 CLI Task
$drop = $this->getApplication()->find('doctrine:database:drop');
$drop_args = array(
'command'=>'doctrine:database:drop',
'--force'=>true
);
$drop->run(new ArrayInput($drop_args), $output);
//Make sure we close the original connection because it lost the reference to the database
$connection = $this->getApplication()->getKernel()->getContainer()->get('doctrine')->getConnection();
if ($connection->isConnected()) {
$connection->close();
}
$create = $this->getApplication()->find('doctrine:database:create');
$created_args = array(
'command'=>'doctrine:database:create'
);
$create->run(new ArrayInput($created_args), $output);
$update = $this->getApplication()->find('doctrine:schema:update');
$update_args = array(
'command'=>'doctrine:schema:update',
'--force'=>true,
'--em'=>'default'
);
$update->run(new ArrayInput($update_args), $output);