Temporarily connecting to an external database using Symfony / Doctrine

Summary:

I want to be able to run a query with an external database to capture some necessary data during user login. I do not want this site to do anything else using an external database. In the long run, you may need to return the data back, but in particular, I do not want symfony to try to build a circuit for external db, it should just leave it alone and allow the connection from time to time.

More details:

I am trying to create a temporary connection to the database of another symfony application and I cannot figure out how to do this.

I have an existing Symfony site that is configured and running. I am trying to create a kind of management system for users of this main site. The management system will have separate deployment options for each user who selects it, so it will also have its own database associated with it. However, these control systems require access to 2 or 3 tables from the main site system.

I tried to add separate entries to databases.yml in the control system to create connections to both databases, however, every time I create them, it wants to put my schema in both databases.

The best idea I came up with is to establish a connection: management in all my tables for the management system, as well as placing the connection: main_site in all tables from the main site. However, this would require me to support yml files both in the control system and on the main site to make sure that they remain current with each other.

Hope this was even a little clear.

Thanks for the help: D

+4
source share
1 answer

To temporarily connect without the possibility of using DQL on a remote site, simply open the connection using Doctrine_Manager :

 $conn = Doctrine_Manager::getInstance()->openConnection('mysql://username: password@localhost /database', 'connection 2', false); 

Make sure the third argument is false so that it does not become the current connection. Then you can run connection requests using PDO:

 $conn->exec('SELECT * FROM table WHERE conditon = ?', array('value')); 

If you want to use DQL and the remote schema, you will need to do it the way you drew: define two connections and maintain the schema in both places (your version control should be able to handle this).

+5
source

All Articles