Snippets

Create an account or login to be able to add, comment and rate snippets.

Navigation

Tutorial: How to make symfony/propel use both Sqlite and Mysql or many database handles

Multiple databases is not really the strongest area of Propel when using symfony, for this it is better recomanded the use of sfDoctrine plugin.

Still, some propel-symfony based projects may require that one or many modules will have to use different database handles, and to do that, first you need to define each database into your database.yml file.

You can have as many dbs you like and on any combination you want, but for this example we will have one mysql db and one sqlite db.

Your database.yml file should look like this:

all:

  database1:
    class:                sfPropelDatabase
    param:
      phptype:            mysql
      hostspec:           localhost
      database:           mysql_databse
      username:           my_username

  database2:
    class:                sfPropelDatabase
    param:
      phptype:            sqlite
      database:           ../data/sqlite_database.db

Once you have the databases defined you need to write the schema for each database you have defined and with this step you can decide which table (model) will use which handle.

In our case we'll have two schema.yml files because we have two dbs and they will look like this:

database1.schema.yml

---
database1:

  table_1:
    _attributes: 
      idMethod: native
    id: 
      type: INTEGER
      required: true
      autoIncrement: true
      primaryKey: true
    value: 
      type: VARCHAR
      size: 32
      required: true

  table_2: 
    _attributes: 
      idMethod: native
    id: 
      type: INTEGER
      required: true
      autoIncrement: true
      primaryKey: true
    value: 
      type: VARCHAR
      size: 32
      required: true

database2.schema.yml

---
database2:

  table_3:
    _attributes: 
      idMethod: native
    id: 
      type: INTEGER
      required: true
      autoIncrement: true
      primaryKey: true
    value: 
      type: VARCHAR
      size: 32
      required: true

Now is time to build the models

$ symfony propel-build-model

If you want to use other symfony commands that uses the database directly like:

$ symfony propel-build-sql
$ symfony propel-insert-sql

You'll have to configure the dbs connection into propel.ini.

Here we have a problem because you can't use both databases with propel.ini, so you'll have to do this one at a time.

database1 on propel.ini

propel.database            = mysql
propel.database.createUrl  = mysql://localhost/
propel.database.url        = mysql://localhost/mysql_database

database2 on propel.ini

propel.database            = sqlite
propel.database.createUrl  = sqlite://./../data/sqlite_database.db
propel.database.url        = sqlite://./../data/sqlite_database.db

Once you have the models generated and the data loaded into your databases, you can play with your databases.yml file.

If you generate the databases for every handle you'll like, you can switch a model from a handle to another.

by Dorin Mirulescu on 2007-06-19, tagged database  propel 

Comments on this snippet

gravatar icon
#1 emgillis on 2007-07-20 at 06:08

Love this tutorial!!

I do have a question about it. I am trying this using two mysql databases.When I end up doing the "propel-insert-sql", I find that: "table_1" and "table_2" will exists in "database_2"; and "table_3" ends up existing in "database_1".

Is there a way to not have this happen?

gravatar icon
#2 emgillis on 2007-07-21 at 08:11

Realized my comment may be a bit confusing... All three tables show up in both dbs. I am wanting to have "table_1" and "table_2" only exist in "databsae_1"; and have "table_3" only exist in "database_2".

gravatar icon
#3 Keith Jones on 2007-09-10 at 07:37

Hi! Got a problem!

I have to use 2 mysql data bases...

When I put their entries in the propel.ini file and wanna use the symfony propel-build-model command, it only recognizes the last database I have especified...

This occurs if I do this in the propel.ini:

propel.database = mysql propel.database.createUrl = mysql://localhost/ propel.database.url = mysql://localhost/mysql_database

propel.database = mysql propel.database.createUrl = mysql://localhost/ propel.database.url = mysql://localhost/mysql_database2

or if I do this:

propel.database = mysql propel.database.createUrl = mysql://localhost/ propel.database.url = mysql://localhost/mysql_database propel.database.url = mysql://localhost/mysql_database2

Any ideas??

Thanks!

gravatar icon
#4 jignesh vishavadia on 2007-10-11 at 02:21

Hi, I am using 2 database and I have make same database.yml as above. all:

database1: class: sfPropelDatabase param: phptype: mysql hostspec: localhost database: mysql_databse username: my_username

database2: class: sfPropelDatabase phptype: mysql hostspec: localhost database: mysql_databse username: my_username

While using such configuration in databases.yml i am facing error like "No connection set for propel".

Please Guide

gravatar icon
#5 jignesh vishavadia on 2007-10-11 at 02:21

Hi, I am using 2 database and I have make same database.yml as above. all:

database1: class: sfPropelDatabase param: phptype: mysql hostspec: localhost database: mysql_databse username: my_username

database2: class: sfPropelDatabase phptype: mysql hostspec: localhost database: mysql_databse username: my_username

While using such configuration in databases.yml i am facing error like "No connection set for propel".

Please Guide

gravatar icon
#6 Gerald Hanks on 2007-10-21 at 04:43

The trick to getting all this to work is NOT to run 'symfony propel-build-all'

Instead run the 'symfony propel-build-model' and the 'symfony propel-build-sql' commands.

Afterwards, execute the sql commands yourself on the correct databases. The propel.ini file only handles a single database connection. The connection string in the databases.yml file are for connecting to the databases during the running of your application. The database config in the propel.ini file in this case is only used during database creation.

gravatar icon
#7 Olivier Mugnier on 2008-12-22 at 04:19

Some trick that help not ending up with all table in one database:

Use the (hidden) shortcut: propel.database.url = mysql://root@localhost/@DB@

Hop that help someone !

gravatar icon
#8 calvin j on 2009-01-07 at 02:35

@jignesh vishavadia I had the same error

In database.yml, add the data source parameter. So in my case, it would look like:

all: pbx: class: sfPropelDatabase param: dsn: '<Your DSN>' datasource: pdx

propel: class: sfPropelDatabase param: dsn: '<Your DSN>'

You need to create an account or log in to post a comment or rate this snippet.