簡體   English   中英

Silex和Doctrine 2中的動態數據庫連接

[英]Dynamic database connection in Silex with Doctrine 2

我正在嘗試執行與此帖子非常相似的操作- 動態數據庫連接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中重新配置數據庫連接。 有沒有人做到這一點?

我在Silex中沒有這樣的請求,但是您可以隨時使用

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

創建與數據庫的連接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM