How to manage Doctrine queries with multiple db schemas

I have an entity A with a ManyToOne relationship with B, but A and B do not belong to the same DB schema.

Entity 'A' belongs to the MyBundle package, and entity 'B' belongs to the MyOtherBundle package.

The official documentation explains how to work with different connections: several schemes = several object managers. But in my case, I would like to join both objects.

Performing:

$this->objEm->getRepository('MyBundle:MyEntity')->find($id); 

or

 $this->objEm->getRepository('MyBundle:MyEntity')->getMyResult($id); 

I only call one of my repositories, and I think it cannot get another, because in my config.yml I can only select one connection.

 doctrine: dbal: connections: connection1: driver: "%database_driver%" host: "%database_host%" port: "%database_port%" dbname: "%database_name%" user: "%database_schema1_user%" password: "%database_schema1_password%" service: "%database_service%" charset: "Windows-1252" connection2: driver: "%database_driver%" host: "%database_host%" port: "%database_port%" dbname: "%database_name%" user: "%database_schema2_user%" password: "%database_schema2_password%" service: "%database_service%" charset: "Windows-1252" orm: entity_managers: em1: connection: connection1 mappings: MyBundle: ~ MyOtherBundle: ~ em2: connection: connection2 mappings: MyOtherBundle: ~ 

Result: Oops, it looks like something went wrong.

1 / 1ReflectionException: class FQCN \ Of \ MyBundle \ Entity \ B does not exist ...

"I know that there is no dude, I want you to look at a good place now: for example, in FQCN \ Of \ MyOtherBundle \ Entity \ B"

How can I force the path to my entity "B"?

+7
source share
2 answers

The problem is solved! This had nothing to do with the database schema or annotations.

In object A, one of my personalized setters forced a type in a parameter:

 public function setB(B $objB) { //... } 

... and I forgot to use B FQCN! That's why he used A.

Next time I will not declare FQCN in annotations to oblige me to use it at the beginning of my class! :)

+2
source

If your schema is in the same database, simply define the tables for the objects as

 Bundle\Entity\Class: type: entity table: schema.class 

(Yaml)

You do not need to specify a second connection. When a diagram is explicitly indicated, these compounds work perfectly in Doctrine 2.

If your schema is in different databases, you have some problems because each connection will query both databases; to combine multiple records, each record will join, meaning that you are executing multiple queries proportional to the number of objects in your result set (BAD).

+3
source

All Articles