3
votes

I'm trying to connect a second database to my project in Symfony2. First, I added into parameters.yml some parameters to create the connection.

Then, I edited my config.yml, and now looks like:

doctrine:
    dbal:
        default_connection:   default
        connections:
            default:
                driver:   pdo_mysql
                host:     "%database_host%"
                port:     "%database_port%"
                dbname:   "%database_name%"
                user:     "%database_user%"
                password: "%database_password%"
                charset:  UTF8
            circutor3:
                driver:   pdo_sqlsrv
                host:     "%database_host_circutor3%"
                port:     "%database_port_circutor%"
                dbname:   "%database_name_circutor%"
                user:     "%database_user_circutor3%"
                password: "%database_password_circutor3%"
                charset:  UTF8
orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true

Finally, I tried to get connected, using the following code in my controller:

$em = $this->getDoctrine()->getManager('circutor3');

And, the error returned by Symfony2:

Doctrine ORM Manager named "circutor3" does not exist.

The circutor3 makes connection to a database, external to my system, so I don't need to create entities or objects. I only need to execute some SELECT to get information and store it using an array.

Is creating a typical mysqli connection the best way to solve my problem? I don't know how to solve this with Symfony. Thank you in advance.

2

2 Answers

4
votes

you could access to the database connection in the controller as follow:

    $connection = $this->getDoctrine()->getConnection('circutor3');

then use the connection as:

$stmt = $connection->prepare($sql);
$stmt->execute();
return $stmt->fetchAll();

Some help here and here

Hope this help

2
votes

According to Symfony documentation (http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html), you only defined a connection, not an entity manager :

You have to create an entity_manager for each connection.

orm:
    default_entity_manager: default
    entity_managers:
        default:
            ...
        circutor3:
            connection: circutor3
            mappings:
                AppBundle: ~