Use two entitymanagers does not work with repository

烂漫一生 提交于 2021-01-28 11:01:04

问题


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

Use different Entity Manager in the Repository of specific Entity objects

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

EventListener

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



...

回答1:


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

service or Controller:

$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;
}


来源:https://stackoverflow.com/questions/59491565/use-two-entitymanagers-does-not-work-with-repository

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!