I've just started working with Propel and I love it, but I have a question regarding how to utilize multiple database connections. I know I can set stuff up in my schema to connect to multiple different databases, but I'm curious how to handle this in code.
The issue I have is multiple databases, and each of them has slightly different schemas with no data warehousing. As a result I have things resembling the following:
databaseName: westCoastUsers
table: users
column1: email
column2: password
column3: FirstName
databaseName: eastCoastUsers
table: users
column1: email
column2: password
column3: firstName
column4: lastName
Right now in PHP non-Propel version, I'm doing all this by hand, and switching databases manually as required. I'm hoping to streamline things a bit, and I'm curious how to model this. Is there a way I can just have something like eastCoastUser and westCoastUser models that each refer to the proper database/etc or am I trying to wedge in something not supported?
I read this: How to use two database in propel but am uncertain how to actually execute that in code.
Thanks for the help
In your schema files, you can provide a name for the class that represents your table. They do not have to use the same name as the table. You do this with the phpName
attribute on the table element.
For example your schema.xml could contain something like this
<database name="westCoastUsers">
<table name="users" phpName="WestCoastUser">
...columns here...
</table>
...
</database>
<database name="eastCoastUsers">
<table name="users" phpName="EastCoastUser">
...columns here...
</table>
</database>
(edit, note that the name="westCoastUser"
on the database element refers to the name of the database, not the classes with similar names)
Then at build time, propel will generate WestCoastUser
, WestCoastUserQuery
, WestCoastUserPeer
, EastCoastUser
, EastCoastUserQuery
and, EastCoastUserPeer
. Each class will connect using the database it was defined under in your schema.