> For the complete documentation index, see [llms.txt](https://docs.ecotone.tech/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ecotone.tech/messaging/multi-tenancy-support/getting-started/symfony-and-doctrine-orm.md).

# Symfony and Doctrine ORM

Multi-tenancy configuration for Symfony and Doctrine ORM

## Configuration with Symfony

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**):

```yaml
doctrine:
  dbal:
    connections:
      tenant_a_connection:
        url: '%env(resolve:TENANT_A_DATABASE_URL)%'
        charset: UTF8
      tenant_b_connection:
        url: '%env(resolve:TENANT_B_DATABASE_URL)%'
        charset: UTF8
  orm:
    entity_managers:
      tenant_a_connection:
        connection: tenant_a_connection
        mappings:
          (Our mapping goes here)
      tenant_b_connection:
        connection: tenant_b_connection
        mappings:
          (Our mapping goes here)
```

and then we can set up Mapping

```php
final readonly class EcotoneConfiguration
{
    #[ServiceContext]
    public function multiTenantConfiguration(): MultiTenantConfiguration
    {
        return MultiTenantConfiguration::create(
            tenantHeaderName: 'tenant',
            tenantToConnectionMapping: [
                'tenant_a' => SymfonyConnectionReference::createForManagerRegistry('tenant_a_connection'),
                'tenant_b' => SymfonyConnectionReference::createForManagerRegistry('tenant_b_connection')
            ],
        );
    }
} 
```

## Sending Message in Context of Tenant

\`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).

```php
final readonly class CustomerController extends Controller
{
    public function __construct(private CommandBus $commandBus) {}
    
    public function placeOrder(Request $request): Response
    {
        $this->commandBus->send(
            new RegisterCustomer($request->get('name')),
            metadata: [
                // This is the Message Header that will be used for mapping tenant
                'tenant' => 'tenant_a'
            ]  
        );
        
        return new Response(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:

```php
final readonly class CustomerService
{
    #[CommandHandler]
    public function handle(
        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:

```php
final readonly class CustomerService
{
    #[CommandHandler]
    public function handle(
        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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.ecotone.tech/messaging/multi-tenancy-support/getting-started/symfony-and-doctrine-orm.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
