1

I have an app that talks to two different databases. For one of my entities, School, I get the following error:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'pressbox.schools' doesn't exist

That makes sense because there is no pressbox.schools. It's fnt.schools. It's trying to talk to the wrong database.

How do I tell my entity which mapping I want it to use? I would of course rather refer to the mappings than to the database names themselves, which can be different depending on the environment.

Jason Swett
  • 43,526
  • 67
  • 220
  • 351

2 Answers2

2

First, declare your connections in a config file (config.yml would be fine):

doctrine:
  dbal:
    default_connection: pressbox # change it as you wish
    connections:
      pressbox:
        driver:   %database_driver%
        host:     %database_host%
        port:     %database_port%
        dbname:   pressbox
        user:     pressbox_usr
        password: pressbox_pwd
        charset:  UTF8
      fnt:
        driver:   %database_driver%
        host:     %database_host%
        port:     %database_port%
        dbname:   fnt
        user:     fnt_usr
        password: fnt_pwd
        charset:  UTF8

Then declare the entity managers:

doctrine:
  orm:
    default_entity_manager: pressbox
    entity_managers:
      pressbox:
        connection: web
      fnt:
        connection: fnt

Now, in a controller, you can tell Doctrine which entity manager to use by passing its name to getEntityManager():

$fntEm = $this->getDoctrine()->getEntityManager('fnt');

Assuming the entity manager for the fnt table is called that same name.

Samy Dindane
  • 17,900
  • 3
  • 40
  • 50
  • I don't know - I'm not actually referring to the entity manager anywhere, so I don't know that this would really be relevant. It happens when I just do `$user->getSchool()` or something like that, which doesn't involve referring to the EM at all. – Jason Swett Jun 12 '12 at 21:16
  • I thought you already configured your entity managers. I'll update my answer. – Samy Dindane Jun 12 '12 at 21:26
  • Updated. I'm not sure, but you might need to map an EM with a bundle like mentioned [here](http://symfony.com/doc/master/cookbook/doctrine/multiple_entity_managers.html). Tell me if that's the case so I update the answer. – Samy Dindane Jun 12 '12 at 21:34
  • Hmm, now that I take a closer look, I realize that I was already doing everything suggested in your answer. If there's a way to tell a bundle to use a certain connection by default, I don't see it on that page you linked. If such a thing could be done, it seems like it would work, though. – Jason Swett Jun 18 '12 at 13:52
  • This answer didn't fix my problem but I'm selecting it since it seems to be the best answer. – Jason Swett Aug 14 '12 at 13:15
  • In case it might help someone, what fixed it for me was the key *connection* that i had forgotten for the entity_managers items... – MediaVince Jul 15 '15 at 13:35
0

i think cross database relations are pretty complicated or you have to code by hand through some plugin.

i found this question about the same topic which might help you:

Multiple database connection in Doctrine2 and Zend framework

Community
  • 1
  • 1
room13
  • 1,922
  • 1
  • 15
  • 28