【发布时间】: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 实体之间的学说关系?如果是这样,那将行不通。无法跨多个实体管理器关联实体。但同样,只是一个猜测。