我是symfony的新手,我想管理多个动态数据库连接
我做的事: 当用户在我的symfony应用程序上注册时,我会为他创建新的学说配置,并具有新的连接和新的实体管理器。 此步骤工作正常;但 之后,我想使用新的配置来创建数据库并使用这些配置更新架构。
当我这样做时,symfony告诉您没有找到new_manager和new_connection
请一些身体可以帮助我吗?
我的代码
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);
}
答案 0 :(得分:0)
如果您使用Symfony,则应使用Symfony的工具简化操作。
答案 1 :(得分:0)
我通过添加新配置后删除缓存解决了我的问题。但是没有Doctrine命令,我用自定义的PHP代码删除了它们