Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2: Connect to additional database, without all the extras?

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:

  1. Put the connection parameters in my config.yml file (and parameters.yml)
  2. Connect do it using Symfony components (Doctrine?) to run prepared statements.

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.

like image 701
Brian Avatar asked Mar 20 '23 20:03

Brian


1 Answers

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.

like image 146
Cerad Avatar answered Apr 27 '23 00:04

Cerad