如何在服务symfony 4中使用多个实体管理器

问题描述 投票:0回答:2

我在doctrine.yml中设置了两个实体管理器我必须将存储库注入服务但问题是存储库始终采用默认实体管理器。我应该如何将特定的实体经理提供给存储库。在symfony 4中,我们可以使用ServiceEntityRepository将存储库视为服务

symfony doctrine
2个回答
2
投票

您可以尝试在构造中注入Doctrine\Common\Persistence\ManagerRegistry。然后使用$managerRegistry->getManager('your_connection_name');

例如:

//use Doctrine\Common\Persistence\ManagerRegistry;
private $connection;
function __construct(ManagerRegistry $em)
{
    $this->connection = $em->getManager('your_connection_name');
}

1
投票

而不是懒惰/自动加载它们,你需要在explicitly configure their loading文件中将每个设置为命名服务和services.yaml

© www.soinside.com 2019 - 2024. All rights reserved.