我想获取存储库中的当前区域设置。这就是为什么我将容器注入到我的存储库中,但我收到错误,我无法弄清楚。
这是我的service.yml
code
survey.repository.container_aware:
class: Demo\SurveyBundle\Repository\SurveyRepository
calls:
- [ setContainer, [ @service_container ] ]
这是我的存储库类代码
.......
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
.......
protected $container;
public function __construct(Container $container) {
$this->container = $container;
}
之后我收到以下错误
ContextErrorException: Catchable Fatal Error: Argument 1 passed to
Demo\SurveyBundle\Entity\SurveyRepository::__construct() must implement
interface Symfony\Component\DependencyInjection\ContainerInterface, instance of
Doctrine\ORM\EntityManager given
我的构造或服务中缺少什么?
您正在传递带有Setter Injection
(在 yml 中)但您在构造函数类中定义它。
顺便说一句,实体管理器已经有一个带参数的构造函数类,所以不要采用Constructor Injection
只需将类中的方法更改为:
public function setContainer(Container $container) {
$this->container = $container;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)