使用两个实体管理器不适用于存储库

编程入门 行业动态 更新时间:2024-10-25 22:26:53
本文介绍了使用两个实体管理器不适用于存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用两个实体管理器来处理两个数据库.问题是我无法为我的存储库设置正确的实体管理器(它使用默认的实体管理器).当我将实体持久化到数据库时,它工作正常(使用 wp entitymanager).如何使用 wp 实体管理器?

I'm using two Entitymanagers to work with two databases. The problem is that I can't set the correct entitymanager for my repository(it uses the default entitymanager). When I persist the entity to the database, it works fine (with the wp entitymanager) . How can I use the wp entitymanager?

问题与此类似.解决方法没用

The problem is similar to this one. The solution didn't work

在特定实体对象的存储库

doctrine.yaml

orm: default_entity_manager: default auto_generate_proxy_classes: true entity_managers: default: connection: default naming_strategy: doctrine.orm.naming_strategy.underscore auto_mapping: true mappings: App: is_bundle: false type: annotation dir: '%kernel.project_dir%/src/Entity' prefix: 'App\Entity' alias: App wp: naming_strategy: doctrine.orm.naming_strategy.underscore connection: wp mappings: Appwp: is_bundle: false type: annotation dir: '%kernel.project_dir%/src/Entitywp' prefix: 'App\Entitywp' alias: Appwp

事件监听器

class CustomSessionListener { function __construct(Security $security,ManagerRegistry $em,WpSessionTableRepository $sessionTableRepository) { $this->security = $security; $this->em = $em; $this->sessionTableRepository = $sessionTableRepository; } public function onKernelController(ControllerEvent $event) { $user = $this->security->getUser(); $manager=$this->em->getManager("wp"); $repository=$manager->getRepository(WpSessionTable::class,"wp"); if(!is_null($user)){//TODO){ $sessionTableObj=new WpSessionTable(); $sessionTableObj=$repository->findByEmail($user->getEmail()); ...

推荐答案

您可以将第二个实体管理器注入您的存储库类,然后使用 createQueryBuilder 来定义您的查询.

you can inject the second entity manager to your repository class and then use createQueryBuilder to define your query.

服务或控制器:

$entityManager = $this->getDoctrine()->getManager('wp'); $WpSessionTable = $entityManager->getRepository(WpSessionTable::class) ->findObjectById($entityManager,$id);

WpSessionTableRepository:

public function findObjectById(EntityManager $em,$id){ $WpSessionTable = $em->createQueryBuilder() ->select('w') ->from(WpSessionTable::class,'w') ->where('w.id = :id') ->setParameter('id',$id) ->getQuery() return $WpSessionTable; }

更多推荐

使用两个实体管理器不适用于存储库

本文发布于:2023-11-27 05:07:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636724.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:管理器   实体   不适用于   两个

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!