【问题标题】:Problem with setting entity manager other than default in user provider for Symfony 5在 Symfony 5 的用户提供程序中设置实体管理器而不是默认值的问题
【发布时间】:2020-04-16 11:24:09
【问题描述】:

我查看了this post,但我的问题有点不同,并且 symfony 安全组件中的许多内容已针对版本 5 进行了更改。

所以,我尝试在用户提供程序中设置默认以外的实体管理器。我使用文档创建了两个连接: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

接下来我更改我的 security.yaml 并添加 manager_name: users - 与文档中的完全一样:

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 }

尽管有上述设置安全组件尝试从默认实体管理器加载类:

[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)"} []

我做错了什么?

【问题讨论】:

  • 首先验证您的用户实体是否正在使用“bin/console dictionary:mapping:info --em=users”进行映射
  • 错误信息包括App\Entity\Main。如果我不得不猜测,您是否试图定义 Main 实体和 Users 实体之间的学说关系?如果是这样,那将行不通。无法跨多个实体管理器关联实体。但同样,只是一个猜测。

标签: symfony doctrine


【解决方案1】:

好的,我想我解决了这个问题。

原因是68行中的类/src/Security/LoginFormAuthenticator.php(由php bin / console make:auth自动生成)是:

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

此行试图从默认实体管理器获取用户。我不知道为什么经理指示没有帮助,例如。 $this->entityManager->getRepository(User::class, 'users')

所以我在 /src/Security/LoginFormAuthenticator.php 中更改了 __constructor,然后我从 ManagerRegistry 获得了实体管理器(它之前是从 EntityManagerInterface 获得的)。这解决了我的登录问题:)

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;
}

希望对大家有用

【讨论】:

  • 很高兴你能成功。这个answer 展示了如何创建一个可以键入提示的 UserEntityManager 并避免使用 ManagerRegistry。只是一个小小的改进。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-29
  • 1970-01-01
相关资源
最近更新 更多