How To: Build Your Schema from Multiple Database Connections

Published 7th January 2008

Multiple database connections are a great feature, but if you plan to generate a schema for each database, you will most certainly run into some problems caused by different connection names. This Howto will show you how to solve the problem.

Connections

Let's start by defining our database connections: open up the file config/databases.yml and create at least two connections with different names (like „database” and „database1”).

all:
  database:
    class:          sfPropelDatabase
    param:
      dsn:          mysql://ptest:ptest@localhost/ptest

database1:
    class:          sfPropelDatabase
    param:
      dsn:          mysql://ptest:ptest@localhost/ptest2</pre>

Schema

In the config/propel.ini configuration file, go to the the sixth line and edit the „propel.database.url” setting so that it matches the first database connection.

propel.database.url = mysql://ptest:ptest@localhost/ptest

Now we can execute the propel-build-schema command.

The next little problem is that the schema generator always assumes that a connection is named „propel” but since we've named it „database”, we need to open the just generated schema file and rename the connection (second line).

Before we go on and generate the second schema, we first need to rename the current one or otherwise symfony would just overwrite it.

mv config/schema.yml config/1_schema.yml

(Note: schema files must end with „schema.yml”)

Second schema

The procedure to generate the schema for the second connection is basically the same as for the first one:

  1. Edit the „propel.database.url” setting in propel.ini.
  2. Execute propel-build-schema.
  3. Open the schema and change the connection name from „propel" to „database1”.
  4. Rename the generated schema.yml to 2_schema.yml.

Finally, we can execute the propel-build-model command and are ready to use our databases.