Skip to main content
Winter Doctrine is a module that provides easy configuration and access to Doctrine ORM and DBAL functionality inside Winter Boot applications.
Framework datasource, migration, and transaction basics are covered in Databases and Datasources, Migrations, and Transactions. This page covers Doctrine ORM/DBAL specifics.

Setup

Install the package:
Enable the module in application.yml:

application.yml

In your application.yml you can configure datasources with a doctrine block under each entry. The example below defines two datasources named defaultdb (primary) and admindb:

Autowired bean naming

ORM and DBAL beans are created automatically and can be autowired by name. The suffix pattern is:

ORM EntityManager

ORM Transaction Managers

DBAL Connection

DBAL Transaction Managers

How to use Transactions

Declarative Transactions (AOP)

Executing something under ORM/DBAL transaction is easy by just using #[Transactional]:

Programmatic Transactions

For fine-grained control, use EmTransactionManager (ORM) or DbalTransactionManager (DBAL) with the getTransaction() / commit() / rollback() pattern.

ORM Example: UserService with EmTransactionManager

DBAL Example: UserDbalService with DbalTransactionManager


Multi-Tenant Support

Winter Doctrine provides native support for multi-tenancy via MultiTenantManager and TenantDataSourceProvider (from Winter Boot), allowing per-tenant EntityManager, Connection, and transaction manager instances.

Configuration via application.yml

You can configure multi-tenant data sources directly in your application.yml by registering a provider class:
When configured, Winter Doctrine automatically initializes and registers a MultiTenantManager bean named <name>-manager (for example, tenantdb-manager) in the application context.

Step 1: Implement TenantDataSourceProvider

Create a #[Configuration] class that returns your implementation of TenantDataSourceProvider:

Step 2: Use in Business Classes

With Multiple MultiTenantManagers

If you have multiple multi-tenant data sources:

API Reference

MultiTenantManager

The MultiTenantManager class manages tenant-specific Doctrine connections and entity managers.
getEntityManager
Returns a cached per-tenant EntityManager.
getConnection
Returns a cached per-tenant Connection.
getEmTransactionManager
Returns a cached per-tenant EmTransactionManager.
getDbalTransactionManager
Returns a cached per-tenant DbalTransactionManager.

Helper Methods