Wednesday, September 18, 2019

Custom user provider using multiple database connections with Doctrine and Symfony 4

Situation :

I am using Symfony 4.3 and autowiring.

I have 2 Doctrine database connections, a default and "users" connection:



doctrine:
    dbal:
        default_connection: default
        connections:

           .....
    orm:
        auto_generate_proxy_classes: true
        default_entity_manager: default
        entity_managers:
            default:
                connection: default
                mappings:
                    Main:
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/src/Entity/Main'
                        prefix: 'App\Entity\Main'
                        alias: Main
            users:
                connection: users
                mappings:
                    Users:
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/src/Entity/UsersManagement'
                        prefix: 'App\Entity\UsersManagement'
                        alias: Users

So my User entity was managed by the secondary connection "users". Checking from command line would show me that everything is mapped correctly:

php bin/console doctrine:mapping:info --em=users

 Found 7 mapped entities:

 [OK]   App\Entity\UsersManagement\User

Also when loading users I am using a customer query by making UserRepository implement the UserLoaderInterface. (https://symfony.com/doc/current/security/user_provider.html).

security:
    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
    providers:
        app_user_provider:
            entity:
                class: App\Entity\UsersManagement\User

But when I try to login, ugly surprise this error pops:
 "The class 'App\Entity\UsersManagement\User' was not found in the chain configured namespaces App\Entity\Main" 

 After googling a little bit I get to this life saving comment from stof: https://github.com/symfony/symfony/pull/8187#issuecomment-18910167


Here is how you configure it:

security:
    providers:
        my_provider:
            entity:
                class: Acme\DemoBundle\Entity\User
                manager_name: users  #non default entity manager

So there is a 'secret' parameter "manager_name" that you need to add to security.yml and is not enough that the entity is correctly mapped by Doctrine.

If somebody ever finds this useful please add a comment :D