Php 如何在循环symfony中运行迁移命令?
在while循环中尝试此操作:Php 如何在循环symfony中运行迁移命令?,php,symfony,migration,Php,Symfony,Migration,在while循环中尝试此操作: $command = array( 'command' => 'doctrine:migrations:execute', '--em' => "dynamic", 'version' => $this->container->getParameter('migration_version') ); $kernel = $this-&
$command = array(
'command' => 'doctrine:migrations:execute',
'--em' => "dynamic",
'version' => $this->container->getParameter('migration_version')
);
$kernel = $this->getContainer()->get('kernel');
$application = new Application($kernel);
$application->setAutoExit(false);
$input = new ArrayInput($command);
$output = new BufferedOutput();
$result = $application->run($input, $output);
$s=$output->fetch();
获取的错误是:
迁移版本20171024105242已在类中注册
条令\DBAL\Migrations\Version
请帮帮我
public function switchDatabase($host, $user, $dbname)
{
//check the company database exist or not
$password = getenv('DEFAULT_DB_PASSWORD');
if (gettype($host) == 'resource') {
$host = long2ip((int) stream_get_contents($host, -1, 0));
}
try {
$conn = new PDO("mysql:host=" . $host . ";dbname=" . $dbname, $user, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn = null;
} catch (\PDOException $e) {
return $e->getMessage();
}
// dynamic DB switch
$connection = $this->getContainer()->get(sprintf('doctrine.dbal.%s_connection', 'dynamic'));
$connection->close();
$refConn = new \ReflectionObject($connection);
$refParams = $refConn->getProperty('_params');
$refParams->setAccessible('public'); //we have to change it for a moment
$params = $refParams->getValue($connection);
$params['dbname'] = $dbname;
$params['host'] = $host;
$params['user'] = $user;
$refParams->setAccessible('private');
$refParams->setValue($connection, $params);
$this->getContainer()->get('doctrine')->resetEntityManager('dynamic');
return true;
}
这是用于db切换的代码db切换工作正常。但是上面的错误是在第二次迁移到while循环中之后出现的。
在while循环中尝试了此操作:
在while循环中您试图实现什么?错误消息很清楚,您已经在当前数据库上运行了迁移20171024105242
,$this->container->getParameter('migration_version')
不会在循环中更改。试试其他的method@Mcsky在我的例子中,切换到while循环中的不同dbs并运行迁移。第一次迁移成功,下一次迁移显示如下$this->container->getParameter('migration_version')是same@sarin.. 但它应该是动态的。。如果尝试两次运行同一迁移,将抛出错误。为什么不运行命令原则:迁移:迁移。。它将自动执行所有迁移。这样做不需要循环