使用理论管理动态数据库连接

问题描述 投票:0回答:1

我是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);
    }
php symfony doctrine
1个回答
0
投票
您可以将文件放在调用此函数的位置并跟踪错误的位置
© www.soinside.com 2019 - 2024. All rights reserved.