gpt4 book ai didi

交响乐 4.x : Doctrine getManagerForClass() not returning the right Entity Manager

转载 作者:行者123 更新时间:2023-12-05 06:34:49 25 4
gpt4 key购买 nike

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

有人指出 doctrine ManagerRegistry 有方法 getManagerForClass,它接受一个类名并且应该返回正确匹配的 Entity Manager。

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

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

如果我检查返回的 entityManager,我发现它正在获取 default 实体管理器,而它应该返回 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
{

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

最佳答案

将“默认”实体管理器放在 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

关于交响乐 4.x : Doctrine getManagerForClass() not returning the right Entity Manager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50030406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com