Generate entity using a given connection

Asked

Viewed 37 times

0

Consider the following configuration file:

config.yml

doctrine:
    dbal:
        default_connection: slqX45
        connections:
            slqX44:
                driver:   pdo_mysql
                host:     "%database_host%"
                port:     "%database_port%"
                dbname:   "%database_name%"
                user:     "%database_user%"
                password: "%database_password%"
                charset:  UTF8
            slqX45:
                driver:   pdo_mysql
                host:     "%database_host_co%"
                port:     "%database_port_co%"
                dbname:   "%database_name_co%"
                user:     "%database_user_co%"
                password: "%database_password_co%"
                charset:  UTF8

Note that the connection marked as default is sqlX45.

However I have some entities that have their respective tables in the database A and others in the database B, so the need to use 2 connections.

When executing the following commands on the console, I need to specify which connection to use and which entity to use, for example:

app/console doctrine:generate:entities --entidade="EntidadeA" --conexao="slqX44" 
app/console doctrine:schema:update --entidade="EntidadeA" --conexao="slqX44"

How to do this?

1 answer

1


When you create Doctrine connection settings within a Symfony project, you can determine different Entity managers for each connection. For each Entity manager, it is possible to discriminate the entities (and consequently tables) will be managed.

Take a look at configuration of Doctrine.

Therefore, I believe that its configuration would look something like this:

doctrine:
    dbal:
        default_connection: slqX45
        connections:
            slqX44:
                driver:   pdo_mysql
                host:     "%database_host%"
                port:     "%database_port%"
                dbname:   "%database_name%"
                user:     "%database_user%"
                password: "%database_password%"
                charset:  UTF8
            slqX45:
                driver:   pdo_mysql
                host:     "%database_host_co%"
                port:     "%database_port_co%"
                dbname:   "%database_name_co%"
                user:     "%database_user_co%"
                password: "%database_password_co%"
                charset:  UTF8
    orm:
        entity_managers:
            em_sqlX44:
                connection: slqX44
                mappings: ~ # os mapeamentos que o EM em_sqlX44 gerenciará
            em_sqlX45:
                connection: slqX45
                mappings: ~ # os mapeamentos que o EM em_sqlX45 gerenciará

After that, just take a look at the arguments accepted by each of the commands. In general, they accept that you specify in which Entity manager that command will run - so, which entities the command will work with.

The argument that commands use is --entity-manager=(seu entity manager).

  • Thanks for your time. In the meantime I have seen the documentation but I can’t find a way to configure which "Entity manager" each entity will be in. I only found way to indicate in the "mappings" which Bundle and in my case the 2 entities belong to the same, but in different databases.

  • You can tell which entities will be part of each manager Ntity, even if they are in the same manager. See the solution to this question: http://stackoverflow.com/questions/9311485/working-with-two-entity-managers-in-the-same-bundle-in-symfony2

Browser other questions tagged

You are not signed in. Login or sign up in order to post.