Mar. 21 2011
1

Setting a Symfony database connection as default

The title of this post is a lie… There’s technically isn’t any way to set a database connection as the default one in Symfony (1.4, anyway). However, there is a nifty trick to making it the default.

What is the default database connection?

In Symfony, what I call the default database connection is the sfDatabase instance returned when calling:

Doctrine_Manager::getInstance()->getConnection();

To get a connection other than the default, just put the name of the connection as the only parameter of getConnection(). This connection is typically used when the model has no connection bound to it.

Setting the default

Like I said, there technically isn’t a way to set the default connection in Symfony 1.4. But, there’s two tricks you can use to do this. Both are pretty grizzly in terms of things you’d want to waste your time on. One is binding a connection to each model and the other is fancy footwork in the databases.yml configuration file.

Binding connections to models

Probably the safest solution to the problem is to bind connections to models. This is just a one liner added to every model class.

Doctrine_Manager::getInstance()->bindComponent('TableClass', 'connection');

For a 4 or 5 table database, this would be a piece of cake to implement. But, I have a 58 table database for my project, so this would be a bitch to implement. Thankfully, if you use Symfony’s schema and model class generators to create your model classes, that work is already done for you in a multi-connection environment.

The binding works well if you don’t use need to call getConnection() and use a “default” connection for things like transactions. If you do, you’ll need to also edit your databases.yml file.

Changing databases.yml

The configuration file databases.yml holds the configuration for each database connection. Typically, if you use the database connection task, you never have to see the file. However, open /config/databases.yml file in a text editor or IDE, copy the whole connection for your default connection, and paste it at the end of the all section.

all:
  connection2:
    class: sfDoctrineDatabase
    param:
      dsn: 'mysql:host=192.168.1.2;dbname=database2'
      user: root
      password: s3cr37
  
  // starting of a connection
  doctrine:
    class: sfDoctrineDatabase
    param:
      dsn: 'mysql:host=127.0.0.1;dbname=database'
      username: root
      password: s3cr37
  // end of a connection

The reason this works is when Symfony parses the databases.yml file, the very last connection gets added as the connection used in getConnection() (or the “default connection”).

I’d also like to bring your attention to the fact the connections are listed in the all section. That means the connection is available to all environments. If you need different parameters for environments, just add a section for that environment and override the connection. How to go about having different connections for different environments is better explained in the Symfony documentation.

While this is a pretty dirty way of doing things–a simple configuration variable to say which connection is “default” would be wonderful–but it gets the job done.

1 comment

  1. December 7, 2015 at 9:20 pm

    hi Matthias Noback ,, i m new to symfony2 with php i just want to know how to make a index.php indsie web dir ,which contain the routing of spefic bundle routing from indsie itself.. can u give me ur gmail id or any other where i can contact to u and can do live chat with u ? ramkesh

Leave a Reply

Previous post:

Next post:

Last.fm interface from fmTuner (modified by me).

Twitter interface from HL Twitter.

Dedicated to my mom, Sharon. May she rest peacefully.
Love you always. (March 23, 1965 - November 12, 2011)

Unsupported Browser!

My site is designed for and currently displays best with modern browsers (and it looks like you don't have one!).

Everything should still function properly, but you won't be able to see all the fancy effects, text, or the way I intended my design to look.

Consider trying the latest version of either Firefox or Chrome... I think you'll like it!

Logan Bibby

P.S.: You can continue viewing my site by clicking the "close" link and you'll never see this again. :)