Tuesday, August 27, 2013

The need to state EntityManager name in a UniqueEntity validation constraint [Symfony2, Doctrine2]

I need to code a backoffice that manages many entities of my sites. Each of my sites has its own bundle with its own database, and a dependency to a common bundle with its own "common" database (for users, logging, billing).

For the same reasons, avoid to access default EntityManager from Container, without naming it... It could lead to an error if you embed your bundle in another application that states another EntityManager as default one...

Doctrine config for site#1

# Doctrine Configuration
doctrine:
    dbal:
        default_connection: site1
        connections:
            site1:
                ...
            common:
                ...
    orm:
        auto_generate_proxy_classes: %kernel.debug%

        default_entity_manager: site1
        entity_managers:
            site1:
                connection: site1
                ...
            common:
                connection: common
                ...

Doctrine config for site#2

# Doctrine Configuration
doctrine:
    dbal:
        default_connection: site2
        connections:
            site2:
                ...
            common:
                ...
    orm:
        auto_generate_proxy_classes: %kernel.debug%

        default_entity_manager: site2
        entity_managers:
            site2:
                connection: site2
                ...
            common:
                connection: common
                ...

Doctrine config for backoffice