rolandow
rolandow

Reputation: 1099

Mapping with multiple databases in Doctrine

I am trying to set up my symfony to use two databases connections. The problem is that Symfony won't map the Entities to the right EntityManager. So perform a query, I have to tell Symfony what manager it should use.

My config:

# Doctrine Configuration
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

        lookup:
          driver:   "%database_driver%"
          host:     "%database_host%"
          port:     "%database_port%"
          dbname:   "Lookup"
          user:     "%database_user%"
          password: "%database_password%"
          charset:  UTF8

  orm:
    auto_generate_proxy_classes: "%kernel.debug%"

    default_entity_manager: default
    entity_managers:
      default:
        connection: default
          mappings:
            MbMyAppBundle: ~

      mvibes:
        mappings:
          MbLookupBundle: ~
        connection: mvibes

So, to query from my Lookup, i have to do: $this->getDoctrine()->getRepository('MbLookupBundle:Country', 'lookup');

Instead, I was hoping I could leave out the second parameter. This way, my bundle would be independent. The project manager can decide what database config he will implement in his project. He'll just have to make sure that the mapping is correct.

How does this work? What are the mappings used for, if this is not possible?

Upvotes: 0

Views: 3817

Answers (1)

AlterPHP
AlterPHP

Reputation: 12727

You have mistakes in connections naming :

# Doctrine Configuration
doctrine:
  dbal:
    default_connection: default
      connections:
        default:
          # ...

        mvibes:   #This is the name of the connection
          # ...

  orm:
    auto_generate_proxy_classes: "%kernel.debug%"

    default_entity_manager: default
    entity_managers:
      default:
        connection: default
        mappings:
          MbMyAppBundle: ~

      mvibes:
        mappings:
          MbLookupBundle: ~
        connection: mvibes #must refre to a connection's name defined above

Official doc : http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html A blog post I wrote about it : http://blog.alterphp.com/2011/10/configuration-trick-for-multiple-entity.html

Upvotes: 1

Related Questions