用条令管理动态数据库连接



我是symfony的新手,我想管理多个动态数据库连接

我做的事:当用户在我的symfony应用程序上注册时,我会为他创建新的条令配置,包括新的连接和新的实体管理器。这一步很好;但是之后,我想使用我的新配置来创建数据库,并使用这些配置更新模式。

当我这样做时,symfony告诉没有找到新的管理器和新的连接

有人能帮我吗?

我的代码

private function create_configs(Request $request, KernelInterface $kernel){
$port = $request->get('_db_port');
$host = $request->get('_db_host');

//getting template
$file = file_get_contents(__DIR__ . "/../../../config/packages/customers_config/template/doctrine_template.txt");
//setting config connection
$file = str_replace("CUSTOMER_DB_CONNECTION","une_co",$file);
//setting config url
$file = str_replace("CUSTOMER_DB_URL","mysql://". $request->get('_username').":". $request->get('_userpassword')."@".$host.":".$port."/bdod?serverVersion=5.7",$file);
//setting config manager
$file = str_replace("CUSTOMER_EM","managerf",$file);
//saving config
$fp = fopen(__DIR__ . "/../../../config/packages/customers_config/some_config.yaml","wb");
fwrite($fp,$file);
fclose($fp);
return $this->create_database($kernel, "une_co", "managerf");
}

private function create_database(KernelInterface $kernel, $connection, $manager){
$dir = __DIR__."../../../";
(new Filesystem)->remove(__DIR__."../../../var/");
$cmd_string = "php". $dir."bin/console";
$application = new Application($kernel);
$application->setAutoExit(false);
$cache  = new ArrayInput([
'command' => 'doctrine:cache:clear-metadata',
]);
$create = new ArrayInput([
'command' => 'doctrine:database:create',
'--connection' => $connection,
'-n' => true,
'--no-debug' => true,
]);
$schema = new ArrayInput([
'command' => 'doctrine:schema:update',
'--force' => true,
'--em' => $manager
]);
// You can use NullOutput() if you don't need the output
$output = new BufferedOutput();
try {
$application->run($cache, $output);
$application->run($create, $output);
$application->run($schema, $output);
} catch (Exception $e) {
dd($e);
}
// return the output, don't use if you used NullOutput()
$content = $output->fetch();
// return new Response(""), if you used NullOutput()
return new Response($content);
}

如果您使用Symfony,您应该使用Symfoy的工具来简化事情。

Symfony 学说

我通过在添加新配置后删除缓存来解决问题。但没有使用条令命令,我用自定义的php代码删除了它们

最新更新