Silex中的动态数据库连接与Doctrine 2

时间:2017-10-13 14:32:01

标签: php symfony doctrine-orm silex

我正在尝试做一些非常类似于这篇文章的内容 - Dynamic database connection symfony2 - 但是与Silex合作。

我已经成功设置了我的基础数据库和我想要动态连接的基础数据库。

database:
  base:
    driver: pdo_sqlite
    path: database/dev.sqlite
  website:
    driver: pdo_sqlite
    path: ~

以上内容被读入$dbs_options,然后使用以下内容进行配置:

    $app -> register(new DoctrineServiceProvider, ['dbs.options' => $dbs_options]);

    // configure the ORM identities
    $app -> register(new DoctrineOrmServiceProvider, [
            'orm.proxies_dir' => Utils::joinPaths($app -> config -> appRoot, 'running', 'proxies'),
            'orm.em.options' => [
                'mappings' => $mappings
            ]
        ]
    );

    // set up multiple entity managers and assign the base connection as default
    $app['orm.ems.default'] = 'basedb';
    $app['orm.ems.options'] = [
        'basedb' => [
            'connection' => 'base',
            'mappings' => $app['orm.em.options']['mappings']
        ],
        'websitedb' => [
            'connection' => 'website',
            'mappings' => $app['orm.em.options']['mappings']
        ]
    ];

在我的before事件中,我能够成功查询basedb以获取我想要连接的网站数据库的名称。

这是我被困的地方,我不知道,也无法找到如何在Silex中重新配置数据库连接。有没有人这样做过?

1 个答案:

答案 0 :(得分:0)

我在Silex中没有这样的请求,但您可以随时使用

$conn = Doctrine\DBAL\DriverManager::getConnection($params, $config);

创建与数据库的连接

相关问题