2

我一直在尝试将我自己的Doctrine\Dbal\Connection对象与 Doctrine Migrations 一起使用,这是我目前得到的,但它一直告诉我提供一个--db-configuration文件,这不是我想要的。

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

例外:

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

1 回答 1

0
$cli->setHelperSet($helperSet);

需要先来

$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 传递给每个命令。

资料来源:我也在为此苦苦挣扎,并仔细研究了代码以查看它是如何工作的,直到我意识到我的错误(这与您的错误相同,并且可能是该问题的未来访问者。)

于 2018-08-25T00:42:55.147 回答