Sep 29
Digg
Stumbleupon
Technorati
Delicious

Dynamic Propel/Doctrine connection

Sometimes you may need to change access to database dynamically inside your project (may be very usefull for sharding approaches). Also wondering what if disable DB access at all from config/settings.yml:

use_database:           false

and create connection only where it’s needed. I’m sure this would speed up performance fantastically. Although almost every page wants to get something from DB πŸ™‚

So it’s not complicated but also not well highlighted in manuals, quick way to create dynamically DB connection in Doctrine:

$db = sfContext::getInstance()->getDatabaseManager()->getDatabase('doctrine');
$db->getDoctrineConnection()->setOption('username', 'yourdbusername');
$db->getDoctrineConnection()->setOption('password', 'yourdbuserpassword');
$db->getDoctrineConnection()->setOption('dsn', "mysql:host=localhost;dbname=yourdbname");
$db->connect();

I’ve also found in snippets this code for Propel (did not test if it would work with latest symfony though):

$dsn = Creole::parseDSN('mysql://localhost/symfony');
$c = Propel::getConfiguration();
$c['datasources'][$name]['connection'] = $dsn;
$c['datasources'][$name]['adapter']    = $dsn['phptype'];
Propel::setConfiguration($c);
Propel::initialize();

So just a quick note for now
Chiao! πŸ™‚


Author: symfonian

8 Comments

Massimiliano Arione
September 29, 2010

It looks pretty useless to me.
Symfony already manages connections and connects to database only when needed, not a microsecond before.
Also, I give you a news: Propel doesn’t use Creole anymore. Since 2 years or so.

CAP
August 6, 2011

@Massimiliano Alone : Not so quick to JUDGE. This code is necessary sometimes. Do not rely on the ability of Symfony or Doctrine alone.

There is a bug in Symfony with connecting to multiple databases where it always selects the last connection first. I am on version 1.l4.12 and STILL experiencing the issue, so it has NOT been resolved yet. Look at this link for more info

http://trac.symfony-project.org/ticket/7689

It refers to this link for a solution which is not perfect either

http://www.funstaff.ch/2010/08/27/multiples-connexions-doctrine-et-le-chargement-des-modeles

CAP
August 6, 2011

@Massimiliano : Not so quick to JUDGE. This code is necessary sometimes. Do not rely on the ability of Symfony or Doctrine alone.

There is a bug in Symfony with connecting to multiple databases where it always selects the last connection first. I am on version 1.l4.12 and STILL experiencing the issue, so it has NOT been resolved yet. Look at this link for more info

http://trac.symfony-project.org/ticket/7689

It refers to this link for a solution which is not perfect either

http://www.funstaff.ch/2010/08/27/multiples-connexions-doctrine-et-le-chargement-des-modeles

symfonian
August 6, 2011

Totally agree and it’s frustraiting that so much symfony/doctrine teams power is dedicated to symfony2/doctrine2 and so less improvements for older version

key tag
July 31, 2012

Symfony already manages connections and connects to database only when needed, not a microsecond before.

key tag
August 1, 2012

there’s a bug in Symfony with connecting to several databases where it always selects the last connection first. I am on version 1.l4.12 and STILL experiencing the issue, so it has NOT been resolved yet. Look at this link for more info

key tag
August 10, 2012

exceedingly agree and it’s frustraiting that so much symfony/doctrine teams power is dedicated to symfony2/doctrine2 and so less improvements for older version

key tag
October 19, 2012

how should we Create the connection which may leads the better performance for all kind of the technical project.

Comments RSS TrackBack Identifier URI

Leave a comment