For Entity Manager while using Doctrine ORM, we can levarage existing Symfony configuration.
We use then register separate Entity Manager per Tenant, as each has it's own unique connection (doctrine.yaml):
`We've defined tenantHeaderName as tenant in our Mapping configuration. This means we can now pass tenant context under this name using Message Headers (metadata).
finalreadonlyclassCustomerControllerextendsController{publicfunction__construct(privateCommandBus $commandBus) {}publicfunctionplaceOrder(Request $request):Response {$this->commandBus->send(newRegisterCustomer($request->get('name')), metadata: [// This is the Message Header that will be used for mapping tenant'tenant'=>'tenant_a' ] );returnnewResponse(200); }}
This way we are telling Ecotone, that we want to execute this Command in context of tenant_a.
Accessing Tenant's Connection
To access current Tenant's Connection, we will be using Atribute:
finalreadonlyclassCustomerService{ #[CommandHandler]publicfunctionhandle(RegisterCustomer $command,// Injecting Connection for current Tenant #[MultiTenantConnection] Connection $connection ):void {// do something }}
By marking Connection with MultiTenantConnection, Ecotone will understand that it should inject Connection for Tenant in current context.
Accessing Tenant's Object Manager
To access current Tenant's Object Manager, we will be using Atribute:
finalreadonlyclassCustomerService{ #[CommandHandler]publicfunctionhandle(RegisterCustomer $command,// Injecting ObjectManager for current Tenant #[MultiTenantObjectManager] ObjectManager $objectManager ):void { $objectManager->persist(Customer::register($command)); }}
By marking ObjectManager with MultiTenantObjectManager, Ecotone will understand that it should inject ObjectManager for Tenant in current context.