Symfony 4.x: Doctrine getManagerForClass() 没有返回正确的实体管理器

Symfony 4.x: Doctrine getManagerForClass() not returning the right Entity Manager

我希望能够在给定实体实例(或仅类名)的情况下获得正确的实体管理器。

有人指出 doctrine ManagerRegistry 有方法 getManagerForClass,它接受类名并且应该 return 正确匹配的实体管理器。

但是,当我调用它时,它总是 return 是默认值。

orm:
    auto_generate_proxy_classes: '%kernel.debug%'
    default_entity_manager: default
    entity_managers:
        default:
            naming_strategy: doctrine.orm.naming_strategy.underscore
            connection: default
            mappings:
                App:
                    is_bundle: false
                    type: annotation
                    dir: '%kernel.project_dir%/src/Entity'
                    prefix: 'App\Entity'
                    alias: App
                gedmo_loggable:
                    is_bundle: false
                    type: annotation
                    dir: '%kernel.project_dir%/vendor/gedmo/doctrine-extensions/lib/Gedmo/Loggable/Entity'
                    prefix: Gedmo\Loggable\Entity
                    alias: GedmoLoggable # this one is optional and will default to the name set for the mapping
        lobbytrack:
            naming_strategy: doctrine.orm.naming_strategy.underscore
            connection: lobbytrack
            mappings:
                Lobbytrack:
                    is_bundle: false
                    type: annotation
                    dir: '%kernel.project_dir%/src/Entity/Lobbytrack'
                    prefix: 'App\Entity\Lobbytrack'
                    alias: Lobbytrack

我正在使用 lobbytrack 数据库中的实体 visitor 进行测试。

$entityClass = 'App\Entity\Lobbytrack\Visitor';
$this->entityManager = $this->managerRegistry->getManagerForClass($entityClass);

如果我检查 returned entityManager,我看到它正在获取 default 实体管理器,而它应该是 returning lobbytrack.

访问者实体的配置将它放在适当的命名空间中,据我所知,这是它如何确定哪个管理器匹配哪个实体。

namespace App\Entity\Lobbytrack;

use Doctrine\ORM\Mapping as ORM;

/**
 * Visitor
 *
 * @ORM\Table(name="visitor")
 * @ORM\Entity(repositoryClass="App\Repository\Lobbytrack\VisitorRepository")
 * @ORM\HasLifecycleCallbacks
 */
 class Visitor
 {

我在想我的配置有什么问题打破了这个,但我看不出是什么。

将 'default' 实体管理器放在 doctrine.yaml 的最后,使其按预期工作。

orm:
    auto_generate_proxy_classes: '%kernel.debug%'
    default_entity_manager: default
    entity_managers:
        lobbytrack:
            naming_strategy: doctrine.orm.naming_strategy.underscore
            connection: lobbytrack
            mappings:
                Lobbytrack:
                    is_bundle: false
                    type: annotation
                    dir: '%kernel.project_dir%/src/Entity/Lobbytrack'
                    prefix: 'App\Entity\Lobbytrack'
                    alias: Lobbytrack
        vcenter:
            naming_strategy: doctrine.orm.naming_strategy.underscore
            connection: vcenter
            mappings:
                Vcenter:
                    is_bundle: false
                    type: annotation
                    dir: '%kernel.project_dir%/src/Entity/Vcenter'
                    prefix: 'App\Entity\Vcenter'
                    alias: Vcenter
        default:
            naming_strategy: doctrine.orm.naming_strategy.underscore
            connection: default
            mappings:
                App:
                    is_bundle: false
                    type: annotation
                    dir: '%kernel.project_dir%/src/Entity'
                    prefix: 'App\Entity'
                    alias: App
                gedmo_loggable:
                    is_bundle: false
                    type: annotation
                    dir: '%kernel.project_dir%/vendor/gedmo/doctrine-extensions/lib/Gedmo/Loggable/Entity'
                    prefix: Gedmo\Loggable\Entity
                    alias: GedmoLoggable # this one is optional and will default to the name set for the mapping