0

I am trying to do something very similar to this post - Dynamic database connection symfony2 - but with Silex.

I have successfully setup my base database and the one I want to connect to dynamically.

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

The above is read into $dbs_options and then the following is used to configure this:

    $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']
        ]
    ];

In my before event I am able to successfully interrogate the basedb to get the name of the website database that I want to connect to.

This is where I am stuck, I do not know and am not able to find how to reconfigure the database connection in Silex. Has anyone done this at all?

Russell Seymour
  • 1,333
  • 1
  • 16
  • 35

1 Answers1

0

I did not have such a request in Silex, but you can always use

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

to create connection to database

ilicmsreten
  • 428
  • 2
  • 5