1

I looked in to this post but my problem is a little different and many things in symfony security component have changed for version 5.

So, I try to set entity manager other than default in user provider. I created two connections using documentation: https://symfony.com/doc/current/doctrine/multiple_entity_managers.html

doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                url: '%env(resolve:DATABASE_URL)%'
            b2b:
                url: '%env(resolve:DATABASE_B2B_URL)%'
            users:
                url: '%env(resolve:DATABASE_USERS_URL)%'

        # IMPORTANT: You MUST configure your server version,
        # either here or in the DATABASE_URL env var (see .env file)
        #server_version: '5.7'
    orm:
        auto_generate_proxy_classes: true
        default_entity_manager: default
        entity_managers:
            default:
                naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
                auto_mapping: false
                connection: default
                mappings:
                    App:
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/src/Entity/Main'
                        prefix: 'App\Entity\Main'
                        alias: Main
            b2b:
                connection: b2b
            users:
                connection: users
                mappings:
                    Users:
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/src/Entity/Users'
                        prefix: 'App\Entity\Users'
                        alias: Users

Next I change my security.yaml and add manager_name: users - exactly as in the documentation:

security:
    encoders:
        App\Entity\Users\User:
            algorithm: auto

    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
    providers:
        # used to reload user from session & other features (e.g. switch_user)
        app_user_provider:
            entity:
                class: App\Entity\Users\User
                property: email
                manager_name: users
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            anonymous: lazy
            provider: app_user_provider
            guard:
                authenticators:
                    - App\Security\LoginFormAuthenticator
            logout:
                path: app_logout
                # where to redirect after logout
                # target: app_any_route

            # activate different ways to authenticate
            # https://symfony.com/doc/current/security.html#firewalls-authentication

            # https://symfony.com/doc/current/security/impersonating_user.html
            # switch_user: true

    # Easy way to control access for large sections of your site
    # Note: Only the *first* access control that matches will be used
    access_control:
        # - { path: ^/admin, roles: ROLE_ADMIN }
        # - { path: ^/profile, roles: ROLE_USER }

Despite the above settings security component try to load class from default entity manager:

[2020-04-16T05:53:55.276798+00:00] request.CRITICAL: Uncaught PHP Exception Doctrine\Persistence\Mapping\MappingException: "The class 'App\Entity\Users\User' was not found in the chain configured namespaces App\Entity\Main" at /home/budmechzz/public_html/rfm.computermedia.com.pl/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/MappingException.php line 23 {"exception":"[object] (Doctrine\\Persistence\\Mapping\\MappingException(code: 0): The class 'App\\Entity\\Users\\User' was not found in the chain configured namespaces App\\Entity\\Main at /home/budmechzz/public_html/rfm.computermedia.com.pl/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/MappingException.php:23)"} []

What am I doing wrong?

  • Start by verifying your User entity is being mapped with "bin/console doctrine:mapping:info --em=users" – Cerad Apr 16 '20 at 11:31
  • The error message includes App\Entity\Main. If I had to guess, you attempted to define a doctrine relation between a Main entity and a Users entity? If so, that will not work. Can't associate entities across multiple entity managers. But again, just a guess. – Cerad Apr 16 '20 at 11:40

1 Answers1

0

Ok, I think I solved the problem.

The reason was the class /src/Security/LoginFormAuthenticator.php (automatically generated by php bin / console make: auth) in 68 line is:

$user = $this->entityManager->getRepository(User::class)->findOneBy(['email' => $credentials['email']]);

This line is trying to get the user from the default entity manager. I do not know why manager indication doesn't help eg. $this->entityManager->getRepository(User::class, 'users')

So I change __constructor in /src/Security/LoginFormAuthenticator.php and I get entity manager from ManagerRegistry (It was previously geted from EntityManagerInterface). This solves my login problem :)

use Doctrine\Common\Persistence\ManagerRegistry;

public function __construct(ManagerRegistry $managerRegistry, UrlGeneratorInterface $urlGenerator, CsrfTokenManagerInterface $csrfTokenManager, UserPasswordEncoderInterface $passwordEncoder)
{
    $this->entityManager = $managerRegistry->getManager('users');
    $this->urlGenerator = $urlGenerator;
    $this->csrfTokenManager = $csrfTokenManager;
    $this->passwordEncoder = $passwordEncoder;
}

I hope it will be useful to someone

  • Glad you got it working. This [answer](https://stackoverflow.com/questions/59266372/how-to-use-symfony-autowiring-with-multiple-entity-managers/59289477#59289477) shows how to make a UserEntityManager that you can type-hint against and avoid the need to use the ManagerRegistry. Just a small refinement. – Cerad Apr 18 '20 at 15:03