【发布时间】:2021-03-06 16:38:50
【问题描述】:
我正在实施 symfony 安全性。我的用户实体在另一个数据库中。我有多个实体经理。到目前为止,我无法让 entitymanager 读取 User 表,在我的情况下这不是默认的 entitymanager。
这是我的 security.yaml
security:
encoders:
App\Entity\User:
id: 'App\Security\Encoder\MyCustomPasswordEncoder'
#app_encoder:
#para oracle
# 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\Trimu\User
property: email
manager_name: trimu
#https://symfony.com/doc/4.0/security/entity_provider.html
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: true
lazy: true
provider: app_user_provider
guard:
authenticators:
- App\Security\LoginFormAuthenticator
logout:
path: app_logout
#target: app_logout
remember_me:
secret: '%kernel.secret%'
lifetime: 2592000 # 30 days in seconds
# 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 }
这是我的教义.yaml
doctrine:
dbal:
default_connection: default
connections:
default:
# configure these for your database server
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
url: '%env(resolve:DATABASE_URL)%'
trimu:
# configure these for your database server
driver: 'oci8'
server_version: ~
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
url: '%env(resolve:DATABASE_SECURITY_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:
default_entity_manager: default
#auto_generate_proxy_classes: true
entity_managers:
default:
connection: default
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
trimu:
connection: trimu
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Trimu'
prefix: 'App\Entity\Trimu'
alias: App
如您所见,我的实体 App\Entity\Trimu\User 位于其他目录和前缀中。我已阅读:Symfony 2.8 : Doctrine getManagerForClass() not returning the right Entity Manager 并且我有同样的问题:getManagerForClass() 方法如何找出哪个实体管理器适合特定类?
谢谢。
已解决,谢谢朋友
我将所有前缀更改为所有目录,现在它可以正常工作了。
doctrine:
dbal:
default_connection: default
connections:
default:
# configure these for your database server
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
url: '%env(resolve:DATABASE_URL)%'
trimu:
# configure these for your database server
driver: 'oci8'
server_version: ~
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
url: '%env(resolve:DATABASE_SECURITY_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:
default_entity_manager: default
#auto_generate_proxy_classes: true
entity_managers:
default:
connection: default
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Mysql'
prefix: 'App\Entity\Mysql'
alias: App
trimu:
connection: trimu
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Trimu'
prefix: 'App\Entity\Trimu'
alias: App
正如史蒂夫所说: 您的第一个实体经理覆盖所有实体,因为:
dir: '%kernel.project_dir%/src/Entity'
【问题讨论】:
-
getManagerForClass 只检查每个实体管理器(按照它们定义的顺序)并选择第一个映射所需实体的管理器。并不总是按预期工作。如果您可以根据以下答案调整映射,那就太好了。如果您需要两个管理器来映射用户实体,那么您将需要手动调整实体管理器注入。这是另一大罐蠕虫。
-
我终于能够解决问题了。必须更改所有目录(通过 dir 参数),因为它们是找到正确实体管理器的关键。我希望这对其他人有帮助。我终于可以通过阅读来理解它:php.developreference.com/article/15121219/…
-
我怎样才能巧妙地附加我正确的教义.yaml 我是stackoverflow的新手
-
点击问题左下角的“编辑”。
-
不要在您的问题中添加 SOLVED 部分,而是考虑提交答案,然后您可以接受它,它可以帮助其他人。否则浏览搜索结果的人会认为它还没有解决。
标签: php symfony security doctrine entitymanager