首页 > 解决方案 > 编辑控制器中的 config.yml 文件

问题描述

出于某些安全原因,每个客户在我的 Symfony 3.4 应用程序中都有自己的数据库,这里是我的 config.yml 文件的示例:

doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                driver:   pdo_pgsql
                host:     127.0.0.1
                port:     null
                dbname:   default
                user:     user
                password: password
                charset:  UTF8
            customer1:
                driver:   pdo_pgsql
                host:     127.0.0.1
                port:     null
                dbname:   customer1
                user:     user
                password: password
                charset:  UTF8
            customer2:
                driver:   pdo_pgsql
                host:     127.0.0.1
                port:     null
                dbname:   customer2
                user:     user
                password: password
                charset:  UTF8

和 ORM :

orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        default_entity_manager: default
        entity_managers:
            default:
                naming_strategy: doctrine.orm.naming_strategy.underscore
                auto_mapping: true
                connection: default
                mappings:
                    BackBundle:  ~
                    BackBundle: ~
            customer1:
                naming_strategy: doctrine.orm.naming_strategy.underscore
                auto_mapping: false
                connection: customer1
                mappings:
                    BackBundle: ~
            customer2:
                naming_strategy: doctrine.orm.naming_strategy.underscore
                auto_mapping: false
                connection: customer2
                mappings:
                    BackBundle: ~

我可以得到 config.yml 文件

$value = Yaml::parseFile('/pathtofile/app/config/config.yml');

$value 包含一个数组,但即使我使用 array_push() 结果也不可读

我的问题:

首先,当管理员在我想执行之后创建一个新用户时,我想在学说中创建一个新的数据库连接,并在 config.yml 文件中自动创建一个新的 orm 配置,doctrine:database:create --connection=dbname并且doctrine:schema:update --force --em=emname 所有这些都在控制器中,而无需手动编辑 config.yml 文件

谢谢

标签: symfonydoctrine-ormorm

解决方案


一个解决方案是用新生成的文件替换actuel config.yml。

像这样 :

use Symfony\Component\Yaml\Yaml;

$value = Yaml::parseFile('/pathtofile/app/config/config.yml');
$value["database_host"] = "10.2.2.1"

file_put_contents('/pathtofile/app/config/config.yml', $value);

文档


推荐阅读