1

I have a few Entity Managers created in my services.yaml, each having its own DBAL connection to different servers. If it matters, we're using TSQL as RDBMS. I pass each of the defined EM's into the constructor of each Entity Repository like:

use App\Entity\Entity1;

  /**
   * Entity1Repository constructor.
   *
   * @param EntityManagerInterface $entityManager Entity Manager
   * @param ManagerRegistry        $registry      Manager Registry
   */
  public function __construct(EntityManagerInterface $entityManager, ManagerRegistry $registry) {
    parent::__construct($registry, Entity1::class);
    $this->_em = $entityManager;
    $this->_entityName = Entity1::class;
  }

and in services.yaml we define this Entity Repository service passing needed EM to it like:

App\Repository\Entity1Repository:
    arguments: ['@em_another']

Now the EM passed fine and I'm able to read from the DB using the custom EM and its DBAL connection. So this works from the controller:

$this->entity1Repository->find(57)->getName()

it returns a string with the name property of the needed entity. Now to the problem. I define a One-to-One relation between an entity located in one DB and Entity1 in question. When trying to lazy load the dependent Entity1 like:

$baseEntity->getEntity1()->getName();

it used the EM passed to the $baseEntity repository constructor and not the one needed to connect and fetch Entity1 data.

TL;DR: how do I make Doctrine lazy loading working for entities located in different DBs and using different DBAL connections?

Alexey
  • 3,414
  • 7
  • 26
  • 44
  • The constructor of EntityManager instantiates a ProxyFactory, which get the same EntityManager in constructor and will be later create the ProxyClasses from Hydrator. So the baseEntity will pass its own EM to all related entities. I see no hooks where you cloud change the EM instance. – Rawburner Sep 04 '19 at 09:03
  • You could use the postLoad LivecycleEvent and overload the loaded Entity1 with the one from different EM. But this would be a performance issue. – Rawburner Sep 04 '19 at 09:04
  • Thanks to you both for your comments! @Rawburner if I attach to this event and inject another entity on postLoad event, will the other relations (that use dependent entities from let's say the same database) be get and set fine? Meaning, if we change the DBAL connection after loading the main entity, won't it affect the other relations of the main one? – Alexey Sep 04 '19 at 11:02
  • You will not change the DBAL connection after the main entity has load, this is afaik not possible because the origin connection is already injected. But you would load the related entity with its right relations from another connection and replace the false entity. This would not affect other related entites in main entitiy, you will go through every related entity which comes from another connection and replace this. – Rawburner Sep 05 '19 at 12:08
  • Does this answer your question? [Symfony 4 EntityManager multiple database](https://stackoverflow.com/questions/57729149/symfony-4-entitymanager-multiple-database) – Elikill58 Apr 25 '23 at 11:49

0 Answers0