Symfony2: connect to an additional database, without any additional features?

I am looking for a connection to another database to run some arbitrary queries, but I do not want to describe the data using entities and repositories, etc. Symfony will not “own” or manage this data. I'm just looking for recommendations on how to:

  • Put the connection parameters in my config.yml file (and parameters.yml)
  • Combine this using Symfony components (Doctrine?) To run prepared statements.

The only similar question I could find is Temporary connection to an external database with Symfony / Doctrine , but this is similar to Symfony 1, since it Doctrine_Managerdoesn’t work in Symfony 2.

+4
source share
1 answer

http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html

Shows how to configure multiple DBAL Doctrine 2 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 give a service called: doctrine.dbal.customer_connection, which you can pull out of the service container.

A DBAL join is a thin wrapper around a standard PHP PDO join 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 a PDO object.

You can also just create a service using the PDO object itself. I do not have an example convenient, but it is easy enough to do. This will completely eliminate the need for doctrine 2.

+7

All Articles