Doctrine Migrations with own database connection instead of using migrations-db.php file

2.1k views Asked by At

I have been trying to use my own Doctrine\Dbal\Connection object with Doctrine Migrations, this is what I got so far, but it keeps telling me to supply a --db-configuration file, which is not what I want.

// CLI script for Doctrine Migrations
$app = require 'bootstrap.php';

$cli = new Symfony\Component\Console\Application('Doctrine CLI');

$cli->addCommands([
    new \Doctrine\DBAL\Migrations\Tools\Console\Command\DiffCommand(),
    new \Doctrine\DBAL\Migrations\Tools\Console\Command\ExecuteCommand(),
    new \Doctrine\DBAL\Migrations\Tools\Console\Command\GenerateCommand(),
    new \Doctrine\DBAL\Migrations\Tools\Console\Command\MigrateCommand(),
    new \Doctrine\DBAL\Migrations\Tools\Console\Command\StatusCommand(),
    new \Doctrine\DBAL\Migrations\Tools\Console\Command\VersionCommand()
]);

$helperSet = new Symfony\Component\Console\Helper\HelperSet([
    // Doctrine\DBAL\Connection $app->getContainer()->db
    'connection' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($app->getContainer()->db),
    'dialog' => new \Symfony\Component\Console\Helper\QuestionHelper(),
]);

$cli->setHelperSet($helperSet);

$cli->run();

Exception:

[InvalidArgumentException]                                                                                 
You have to specify a --db-configuration file or pass a Database Connection as a dependency to the Migrat  
ions.  
1

There are 1 answers

0
Willem Renzema On
$cli->setHelperSet($helperSet);

needs to come before

$cli->addCommands([
    new \Doctrine\DBAL\Migrations\Tools\Console\Command\DiffCommand(),
    new \Doctrine\DBAL\Migrations\Tools\Console\Command\ExecuteCommand(),
    new \Doctrine\DBAL\Migrations\Tools\Console\Command\GenerateCommand(),
    new \Doctrine\DBAL\Migrations\Tools\Console\Command\MigrateCommand(),
    new \Doctrine\DBAL\Migrations\Tools\Console\Command\StatusCommand(),
    new \Doctrine\DBAL\Migrations\Tools\Console\Command\VersionCommand()
]);

so that the helperSet is passed to each of the commands.

Source: I was struggling with this too, and dug through the code to see how it worked until I realized my mistake (which was the same as yours, and possible future visitors to this question.)