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

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

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

If in site#1 I handle this entity with UniqueEnity constraint, I need to state entityManager name if I don't want to get an error like "Call to a member function getClassMetadata() on a non-object" (Doctrine does not throw Exception Registry::getManagerForClass($class) gives no result).


namespace AlterPHP\Site1Bundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * AlterPHP\Site1Bundle\Entity\EntityForSite1
 * @ORM\Entity() *
 * @UniqueEntity(fields={"slug"}, em="site1")
*/
class EntityForSite1
{
    ...
}

2 comments :

Unknown said...

Hi,
is it possible to specify the em dynamically ?

AlterPHP said...

As far as I know, I think it's not possible.

Post a Comment

Comments are moderated before being published.