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?
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With