gpt4 book ai didi

doctrine-orm - 如何将 migrations_paths 传递给 doctrine :migrations:migrate?

转载 作者:行者123 更新时间:2023-12-05 05:59:47 28 4
gpt4 key购买 nike

可以定义存储迁移的多个路径:

doctrine_migrations.yaml

doctrine_migrations:
migrations_paths:
'App\Migrations': '%kernel.project_dir%/src/App'
'AnotherApp\Migrations': '/path/to/other/migrations'
'SomeBundle\Migrations': '@SomeBundle/Migrations'

现在我想分别使用这些路径,像这样:

$ php bin/console doctrine:migrations:migrate --em=foo --migrations_paths="AnotherApp\Migrations"

但是没有 migrations_paths 参数,而且我也没有找到其他任何东西,这听起来像是我需要的。

如何将单个路径或路径列表传递给 doctrine:migrations:migrate

最佳答案

我很确定你可以在 advanced section 中做到这一点在他们的文档中,提到了一种定义文件 cli-config.php 的方法。那么是什么阻止你定义这样一个内容如下的文件:

<?php
require 'vendor/autoload.php';

use Doctrine\DBAL\DriverManager;
use Doctrine\Migrations\Configuration\Connection\ExistingConnection;
use Doctrine\Migrations\DependencyFactory;
use Doctrine\Migrations\Configuration\Migration\YamlFile;

// $config = new PhpFile('migrations.php'); // Or use one of the Doctrine\Migrations\Configuration\Configuration\* loaders
$config = new YamlFile('database.yml');

$conn = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'memory' => true]);

return DependencyFactory::fromConnection($config, new ExistingConnection($conn));

所以现在你有一个地方可以在进一步传递之前更改 $config,然而,为了让它看起来非常干净,我们可以使用装饰器模式来定义我们的配置版本像这样:

<?php
class CLIDecorator extends ConfigurationFile
{
/**
* @var YamlFile
*/
private $config;

private $args;

public function __construct(YamlFile $file, $args)
{
$this->config = $file;
$this->args = $args;
}

public function getConfiguration(): Configuration
{
$config = $this->config->getConfiguration();
// I'm not sure if php has a nicer argv parser
// but I'm leaving this up to the reader
if ($this->getMigrationOpts()) {
$config['migrations_paths'] = $this->parse($this->getMigrationOpts());
}

return $config;
}

final protected function parse($rawOpt)
{
$parts = explode("=", $rawOpt);
return str_replace('"', '', $parts[1]);
}

final protected function getMigrationOpts()
{
foreach ($this->args as $arg) {
if (str_starts_with($arg, '--migrations_paths')) {
return $arg;
}
}

return null;
}
}

最后我们的代码看起来像这样:

<?php
$config = new CLIDecorator(new YamlFile('database.yml'), $argv);

$conn = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'memory' => true]);

return DependencyFactory::fromConnection($config, new ExistingConnection($conn));

关于doctrine-orm - 如何将 migrations_paths 传递给 doctrine :migrations:migrate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67970233/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com