Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple variable connections with Propel and Symfony2

Tags:

symfony

propel

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?

like image 389
Stan Avatar asked Jul 09 '14 10:07

Stan


1 Answers

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();
like image 162
Jordi Avatar answered Oct 18 '22 20:10

Jordi