13

I have a Symfony2 project with a MySQL db:

#app/config/config.yml
doctrine:
    dbal:
        driver:   %database_driver%    # <
        host:     %database_host%      # |
        port:     %database_port%      # | Defined in
        dbname:   %database_name%      # | parameters.ini
        user:     %database_user%      # |
        password: %database_password%  # <

    orm:
        auto_generate_proxy_classes: %kernel.debug%
        auto_mapping: true

Now I'd like to make simple queries (like routine calls) to an other database.

Should I define an other dbal into the config file ?
If yes, how can it be configured while keeping the default connection for the project ?
Do I have to configure an orm for each connection ?

Pierre de LESPINAY
  • 44,700
  • 57
  • 210
  • 307

1 Answers1

22

You need to add another level of configuration and also use multiple entity managers as Doctrine uses 1 entity manager per database connection .. your configuration might look something like this :

doctrine:
    dbal:
      connections:
        default:
          driver:   %database_driver%    # <
          host:     %database_host%      # |
          port:     %database_port%      # | Defined in
          dbname:   %database_name%      # | parameters.ini
          user:     %database_user%      # |
          password: %database_password%  # <
        another:
          driver:   %database2_driver%    # <
          host:     %database2_host%      # |
          port:     %database2_port%      # | Defined in
          dbname:   %database2_name%      # | parameters.ini
          user:     %database2_user%      # |
          password: %database2_password%  # <

Then you define your multiple entity managers as so

doctrine:
    orm:
        default_entity_manager:   default
        entity_managers:
            default:
                connection:       default
                mappings:
                    AcmeDemoBundle: ~
                    AcmeStoreBundle: ~
            another:
                connection:       another
                mappings:
                    AcmeCustomerBundle: ~

then in your action you can use the following to get the correct entity manager :

$em = $this->get('doctrine')->getEntityManager('default');
$em = $this->get('doctrine')->getEntityManager('another');

depending on which entity manager you required

Manse
  • 37,765
  • 10
  • 83
  • 108
  • Exactly what I was searching for. BTW must I define an entity manager for each connection ? I will only call routines from the other connection. – Pierre de LESPINAY Jul 02 '12 at 10:41
  • You need an entity manager per connection yes ... if you are not going to use it why are you setting up the connection ? – Manse Jul 02 '12 at 10:44
  • In fact before my `another` connection, I was using `auto_mapping: true` and I don't need to manage any entity from the `another` connection. I suppose I will have to use `$this->get('doctrine.dbal.another_connection')` and keep the `entity_managers` empty – Pierre de LESPINAY Jul 02 '12 at 10:48
  • @PierredeLESPINAY you could try it ... not sure to be honest ... but let me know if that works like that ... – Manse Jul 02 '12 at 12:41
  • Seems to work. I just kept `auto_mapping: true` and it seems to be still well mapped for main data. – Pierre de LESPINAY Jul 02 '12 at 12:51
  • @ManseUK since you suggest configuring the database connections in parameters.ini I wanted to know how to set the db_host in a dev/prod environment as I normally would configure these settings in the confid_dev.yml and config_prod.yml and use config.yml for all generic settings – Phill Pafford Nov 29 '12 at 21:08
  • @PhillPafford parameters.ini/yml is unique for each environment - its not stored in CVS – Manse Nov 29 '12 at 22:09
  • @ManseUK Please see my question: http://stackoverflow.com/questions/13647245/confusion-with-parameters-yml-and-config-devstageprod-yml-for-database-conne – Phill Pafford Nov 30 '12 at 15:47