
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!
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.
@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
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
Comments RSS TrackBack Identifier URI
Leave a comment









3 Comments