我在doctrine.yml
中设置了两个实体管理器我必须将存储库注入服务但问题是存储库始终采用默认实体管理器。我应该如何将特定的实体经理提供给存储库。在symfony 4中,我们可以使用ServiceEntityRepository
将存储库视为服务
您可以尝试在构造中注入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');
}
而不是懒惰/自动加载它们,你需要在explicitly configure their loading文件中将每个设置为命名服务和services.yaml
。