Multiple variable connections with Propel and Symfony2
Asked Answered
B

1

6

I'm building an application in Symfony2 where every user gets its own database, meaning all users have their own set of database credentials. The user doesn't know those credentials, they are stored within the application.

Depending on which user is logged in, the application retrieves the user specific credentials and stores data in the user specific database.

I'm using Propel as ORM and I know I can set up multiple connections. But all the solutions I came across require knowing the connection details on beforehand, but I do not know what user will register and log in.

So my question is: How I can I initiate the proper database connection?

Bartie answered 9/7, 2014 at 10:34 Comment(0)
A
5

Supposing you already have connection (if needed, to a dummy database), you can change your connection parameters doing the following

    // Get current configuration
    $config = \Propel::getConfiguration();

    // Change DB configuration
    $config['datasources']['default']['connection']['dsn'] = 'mysql:host=127.0.0.1;port=3306;dbname=dbname;charset=UTF8';
    $config['datasources']['default']['connection']['user'] = 'username';
    $config['datasources']['default']['connection']['password'] = 'password';

    // Apply configuration
    \Propel::setConfiguration($config);
    \Propel::initialize();
Amply answered 19/7, 2014 at 15:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.