I've setup my Symfony project with multiple databases, in the hope to one day implement a modular monolith infrastructure.
My User class is stored in a specific database which is not doctrine default.
I've set this mapping in doctrine_mongodb.yaml
:
document_managers:
authentication:
connection: authentication
database: '%env(resolve:AUTHENTICATION_MONGODB_DB)%'
mappings:
Cobredia\Authentication:
is_bundle: false
type: attribute
dir: '%kernel.project_dir%/src/Authentication/Document'
prefix: 'Organization\Authentication\Document'
alias: 'Organization\Authentication'
SonataUserBundle: ~
The mapping is functional, this command works:
symfony console sonata:user:create --super-admin admin name@domain.com password
I can use the created user to login in the Sonata Admin interface. The web UI is able to list users, but when trying to add a user using the web UI, the follow error is thrown:
request.CRITICAL: Uncaught PHP Exception Symfony\Component\Validator\Exception\ConstraintDefinitionException:
"Unable to find the object manager associated with an entity of class "Organization\Authentication\Document\User"."
at vendor/symfony/doctrine-bridge/Validator/Constraints/UniqueEntityValidator.php line 81 {"exception":"[object] (Symfony\\Component\\Validator\\Exception\\ConstraintDefinitionException(code: 0): Unable to find the object manager associated with an entity of class \"Organization\\Authentication\\Document\\User\". at vendor/symfony/doctrine-bridge/Validator/Constraints/UniqueEntityValidator.php:81)"} []
The issue is due to Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity
defined in vendor/sonata-project/user-bundle/src/Resources/config/validation.xml
.
By default, it use service doctrine.orm.validator.unique
. For MongoDB, it should be doctrine_odm.mongodb.unique
.
How can I fix this cleanly ?
Thank you.
I've tried to add specific Unique constraints to my user class to try to override existing constraints:
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Sonata\UserBundle\Document\BaseUser;
use Symfony\Component\Validator\Constraints as Assert;
#[ODM\Document()]
#[UniqueEntity(fields: "username", ignoreNull: false, em: 'authentication', service: 'doctrine_odm.mongodb.unique')]
#[UniqueEntity(fields: "email", ignoreNull: false, em: 'authentication', service: 'doctrine_odm.mongodb.unique')]
class User extends BaseUser
{
The added constraints seems to work, but the issue remains.