使用Symfony3.3将存储库注入服务时出错 [英] Error injecting repository into service with Symfony3.3

查看:108
本文介绍了使用Symfony3.3将存储库注入服务时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含服务层和存储库层的应用程序。对于服务和存储库,我都声明了interface,并在控制器中注入了服务的接口,并向服务注入了存储库的接口。

I have an application that consists of a service layer and a repository layer. For both the services and the repositories I have declared interface and I inject the interface for services in the controller and services get injected with interfaces of the repositories. This is all done with autowire set to true.

当我在注入的服务之一上调用方法时,只要我不调用需要注入的存储库之一的功能。当我尝试调用使用存储库之一的函数时,出现以下错误:

When I call a method on one of the injected services it all works fine, as long as I don't call a function that needs one of the injected repositories. When I try to call a function that uses one of the repositories I get the following error:


无法自动装配服务 AppBundle\Repository\ \TestRepository:方法 Doctrine\ORM\EntityRepository :: __ construct()的参数 $ em必须具有类型提示或明确地赋予其值。

Cannot autowire service "AppBundle\Repository\TestRepository": argument "$em" of method "Doctrine\ORM\EntityRepository::__construct()" must have a type-hint or be given a value explicitly.

现在,我认为这与必须从我的存储库扩展的EntityRepository类有关,因为当我查看构造函数时,它看起来像这样:

Now I figured that this has to do with the fact that this has to to with EntityRepository class from which my repositories extend, cause when I look at the constructor it looks like this:

class TestRepository extends EntityRepository implements TestRepositoryInterface
{
public function __construct(
            EntityManager $em,
            Mapping\ClassMetadata $class
        ) {
            parent::__construct($em, $class);
        }

    /**
     * @return string
     */
    public function getTest(): string
    {
        return 'This is a test';
    }
}

其中显然包含在中提到的$ em参数错误消息。我不知道如何解决这个问题。
当前,我的服务和存储库在services.yml中配置相同,但是由于服务似乎可以正常工作,所以我认为这不是问题。我是否需要为存储库禁用自动装配功能,并在services.yml中手动配置它们,还是只是缺少真正明显的功能?

Which clearly contains the $em parameter that is mentioned in the error message. I have just no clue how to solve this. Currently my services and repositories are configured the same in the services.yml, but since the services seem to work, I would think that is not the problem. Do I need to disable autowire for my repositories and configure them manually in the services.yml, or am I just missing something really obvious?

推荐答案

存储库无法直接实例化。您需要使用EntityManager :: getRepository

Repositories cannot be directly instantiated. You need to use EntityManager::getRepository

所以您需要在services.yml中定义存储库。

So you will need to define your repos in services.yml

// services.yml
AppBundle\Repository\UserRepository:
    factory: 'doctrine.orm.entity_manager:getRepository'
    arguments: ['AppBundle\Entity\User']

然后自动接线注入应该起作用。

And then autowire injection should work.

我很好奇,看看autowire是否真的流行起来。从本质上来说,这令人沮丧,因为某些服务好像是魔术般地连接在一起,而其他服务则需要人工干预,这可能会导致混乱。

I'll be curious to see if autowire really does catch on. It is inherently frustrating since some services are wired as if by magic while others require manual intervention which could result in a bit of a mess.

这篇关于使用Symfony3.3将存储库注入服务时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆