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?