I'm looking to Connect to another database to run some arbitrary queries, but don't want to describe the data using Entities and Repositories and the like. Symfony will not "own" this data or manage anything about it. I'm simply looking for recommendations on how to:
The only similar question I could find was Temporary Connection to External Database with Symfony/Doctrine, but that seems to apply to Symfony 1 since Doctrine_Manager
doesn't exist in Symfony 2.
http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html
Shows how to setup multiple Doctrine 2 DBAL connections (ignore the entity manager part).
doctrine:
dbal:
default_connection: default
connections:
default:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
customer:
driver: "%database_driver2%"
host: "%database_host2%"
port: "%database_port2%"
dbname: customer
user: "%database_user2%"
password: "%database_password2%"
charset: UTF8
This will yield a service called: doctrine.dbal.customer_connection which you can pull from the service container.
The DBAL connection is a thin wrapper around the standard PHP PDO connection object.
http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/data-retrieval-and-manipulation.html
Shows how to use the connection. Basically the same as the PDO object.
You could also just create a service using the PDO object itself. I don't have an example handy but it's easy enough to do. That would eliminate the need for Doctrine 2 completely.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With