0

我已将“Doctrine\ORM\EntityManager”添加为服务并公开可见:

在此处输入图像描述

但我收到了这个错误:

在此处输入图像描述

我想在我的控制器中注入 EntityManager。怎么了?

4

1 回答 1

2

你得到错误““Doctrine\ORM\EntityManager”:它的构造函数必须是公共的。” , 因为默认情况下 symfony 尝试使用它的名字作为类来创建服务,如果没有定义工厂则调用它的构造函数。Doctrine\ORM\EntityManager:__construct 是 protected,所以你不能在这个类之外使用新的 EntityManager 或者只能从一个扩展它的类中使用。

您应该使用create factory 方法并提供其参数:

parameters:
    doctrine.orm.entitymanager.factory: Doctrine\ORM\EntityManager

services:
    Doctrine\ORM\EntityManager:
        class: Doctrine\ORM\EntityManager
        factory:   ["%doctrine.orm.entitymanager.factory%", "create"]
        arguments:
             $connection: "provide the connection also here"
             $config: "provide the config also here"
        public: true

类:Doctrine\ORM\EntityManager在这种情况下不需要,如果省略,类将默认为服务名称。

您还必须将工厂参数($connection & config)定义为服务并将它们提供给工厂。

PS:我不知道您要做什么,但也许您正在寻找的东西已经可以使用Doctrine Bundle完成

于 2017-12-18T21:43:59.493 回答