I am working on a project using Propel ORM and need to connect to two different databases to retrieve certain data. I've attempted searching online for a guide, but had no luck as all the "solutions" were either specific to Symphony or just simply did not work.
I would like to be able to have two connections specified and be able to switch between them during certain queries by passing a connection alias to the query. However, it is also acceptable to have both connections open simultaneously.
Any and all help is greatly appreciated!
Thanks, Dima
According to the Propel API Docs you can define a database connection in your schema.xml file in such a manner:
<?xml version="1.0" encoding="UTF-8"?>
<database name="bookstore" defaultIdMethod="native">
<!-- table definitions go here -->
</database>
Then in your runtime-conf.xml settings you can set the connection params:
<datasources default="bookstore">
<datasource id="bookstore">
<adapter>mysql</adapter> <!-- sqlite, mysql, mssql, oracle, or pgsql -->
<connection>
<dsn>mysql:host=localhost;dbname=my_db_name</dsn>
<user>my_db_user</user>
<password>my_db_password</password>
</connection>
</datasource>
</datasources>
-- Edit --
Doesn't appear that Propel supports an alias attribute for the database tag, but you should be able to get away with the following:
<?xml version="1.0" encoding="UTF-8"?>
<database name="bookstore_primary" defaultIdMethod="native">
<!-- table definitions go here -->
</database>
<database name="bookstore_secondary" defaultIdMethod="native">
<!-- table definitions go here -->
</database>
<datasources default="bookstore_primary">
<datasource id="bookstore_primary">
<adapter>mysql</adapter> <!-- sqlite, mysql, mssql, oracle, or pgsql -->
<connection>
<dsn>mysql:host=localhost;dbname=bookstore_primary</dsn>
<user>my_db_user</user>
<password>my_db_password</password>
</connection>
</datasource>
</datasources>
<datasources default="bookstore_secondary">
<datasource id="bookstore_secondary">
<adapter>mysql</adapter> <!-- sqlite, mysql, mssql, oracle, or pgsql -->
<connection>
<dsn>mysql:host=localhost;dbname=bookstore_secondary</dsn>
<user>my_db_user</user>
<password>my_db_password</password>
</connection>
</datasource>
</datasources>
See if that works. At the end of the day the thing that really matters is that a connection can be made to both databases (using respective DSNs).