4

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.

1
  • well it sounds u just need the abstraction layer for doctrine instead of the full ORM. Just create few services that contain your custom queries and off you go :) Commented Jan 22, 2014 at 16:30

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Exactly what I needed. I was a little standoff-ish from the Symfony docs because they all seemed to imply Symfony would own the data of any additional database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.