0

Context

I'm developing a website where I have to use data from two distinct databases (one local with full access, one external on read only).

One of "local" entities needs mapping to an "external" entity.
The external entity won't have its data changed since I can't persist these to the DB anyway.

Question

Is there a way to mark this mapping so that the external entity is pulled along when I retrieve the local entity ?

Community
  • 1
  • 1
Michael De Keyser
  • 787
  • 1
  • 17
  • 45

1 Answers1

2

Short answer, No.

You can setup multiple database connections and use the same entity classes for both of them. But a single entity will not be able to have properties that map to different databases. You may have reference fields on there but those will need to just be keys that you can use to look them up using the other connection. For example imagine the following setup:

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
            customer:
                driver:   '%database_driver2%'
                host:     '%database_host2%'
                port:     '%database_port2%'
                dbname:   '%database_name2%'
                user:     '%database_user2%'
                password: '%database_password2%'
                charset:  UTF8

    orm:
        default_entity_manager: default
        entity_managers:
            default:
                connection: default
                mappings:
                    AcmeBundle:  ~
            customer:
                connection: customer
                mappings:
                    AcmeBundle: ~

Both managers will use the entity classes in the AcmeBundle. Then you can do something like

public function someControllerAction(){
    // Get customer from the default connection
    $customer = $this->getDoctrine()
                     ->getManager() // If no value is provided the default is implied
                     ->getRepository('AcmeBundle:Customer')
                     ->findOneBy([
                         'id'=>12
                     ]);

    // Get the customers details from another connection
    $customerDetails = $this->getDoctrine()
                            ->getManager('customer')
                            ->getRepository('AcmeBundle:CustomerDetails')
                            ->findOneBy([
                                'customer_details_id' => $customer->getDetailsId()
                            ]);
    ...
}
Chase
  • 9,289
  • 5
  • 51
  • 77