2

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.  
jedrzej.kurylo
  • 39,591
  • 9
  • 98
  • 107
Robbe
  • 192
  • 2
  • 13
  • Good question. I stumbled upon a problem to run migrations from my tests, I have already established an in-memory storage and connected to it (via doctrine dbal). Now I need to reuse that connection to fill up the contents. – Dmitriy Lezhnev Apr 15 '17 at 14:59

1 Answers1

0
$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.)

Willem Renzema
  • 5,177
  • 1
  • 17
  • 24