Symfony 3.3将存储库注入服务 [英] Symfony 3.3 injecting repositories into services

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

问题描述

我有一个捆绑软件,该捆绑软件保存在一个私有的Satis存储库中,因为它的实体和存储库在多个应用程序之间共享。

I have a bundle which is held in a private Satis repository as its entities and repositories are shared between multiple application.

使用该捆绑软件的其余应用程序是Symfony 2.7和2.8应用程序。我正在开发一个新应用程序,要求是使用Symfony 3.3。

The rest of the applications that consume that bundle are Symfony 2.7 and 2.8 applications. I'm working on a new application and the requirement is to use Symfony 3.3.

在symfony 3.3应用程序中,我已经在services.yml中尝试过此操作:

In the symfony 3.3 application I have tried this in my services.yml:

# Learn more about services, parameters and containers at
# http://symfony.com/doc/current/service_container.html
parameters:
    #parameter_name: value

services:
# default configuration for services in *this* file
    _defaults:
        autowire: true
        autoconfigure: true
        # this means you cannot fetch services directly from the container via $container->get()
        # if you need to do this, you can override this setting on individual services
        public: false

    # makes classes in src/AppBundle available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    CbmLtd\UvmsBundle\:
        resource: '../../vendor/cbmltd/uvms-bundle/*'
        exclude: '../../vendor/cbmltd/uvms-bundle/{Entity,Repository}'

    UvmsApiV1Bundle\:
        resource: '../../src/UvmsApiV1Bundle/*'
        exclude: '../../src/UvmsApiV1Bundle/{Entity,Repository}'

上面给出了以下异常:


无法自动装配服务 UvmsApiV1Bundle\Service\DealerService :方法 __construct()的参数 $ repository引用类 CbmLtd\UvmsBundle\Repository\DealerRepository,但不存在此类服务。它不能自动注册,因为它来自不同的根名称空间。

Cannot autowire service "UvmsApiV1Bundle\Service\DealerService": argument "$repository" of method "__construct()" references class "CbmLtd\UvmsBundle\Repository\DealerRepository" but no such service exists. It cannot be auto-registered because it is from a different root namespace.

好,所以我想我需要显式声明此存储库作为服务。我在Symfony 3.3文档中找不到任何有关将存储库声明为服务的内容,并且2.8语法也不起作用。

Ok, so I guess I need to explicitly declare this repository as a service. I cannot find anything in Symfony 3.3 documentation about declaring a repository as a service, and the 2.8 syntax doesn't work either.

我将其添加到我的services.yml中:

I added this to my services.yml:

services:
    ...
    CbmLtd\UvmsBundle\DealerRepository:
        class: CbmLtd\UvmsBundle\Entity\DealerRepository
        factory_service: doctrine.orm.entity_manager
        factory_method: getRepository
        arguments:
            - CbmLtd\UvmsBundle\Entity\Dealer

但我仍然会遇到此异常:

But I still get this exception:


无法自动装配服务 UvmsApiV1Bundle\Service\DealerService:方法 __construct()的参数 $ repository引用类 CbmLtd\UvmsBundle\Repository\DealerRepository,但没有此类服务存在。由于它来自不同的根名称空间,因此无法自动注册。

Cannot autowire service "UvmsApiV1Bundle\Service\DealerService": argument "$repository" of method "__construct()" references class "CbmLtd\UvmsBundle\Repository\DealerRepository" but no such service exists. It cannot be auto-registered because it is from a different root namespace.

我无法对CbmLtd\UvmsBundle进行任何更改,因为由多个应用程序使用。任何帮助,将不胜感激。我确实花了几个小时在此上,这非常令人沮丧。

I cannot make any changes to CbmLtd\UvmsBundle as this is used by multiple applications. Any help would be appreciated. I've literally spent hours on this, it's very frustrating.

推荐答案

我能够使用以下services.yml做到这一点。 :

I was able to do this using the following services.yml:

services:
# default configuration for services in *this* file
    _defaults:
        autowire: true
        autoconfigure: true

    # this means you cannot fetch services directly from the container via $container->get()
    # if you need to do this, you can override this setting on individual services
    public: false

    # makes classes in src/AppBundle available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    CbmLtd\UvmsBundle\:
        resource: '../../vendor/cbmltd/uvms-bundle/*'
        exclude: '../../vendor/cbmltd/uvms-bundle/{Entity,Repository}'

    CbmLtd\UvmsBundle\Repository\DealerRepository:
        factory: doctrine.orm.entity_manager:getRepository
        arguments:
            - CbmLtd\UvmsBundle\Entity\Dealer

    UvmsApiV1Bundle\:
        resource: '../../src/UvmsApiV1Bundle/*'
        exclude: '../../src/UvmsApiV1Bundle/{Entity,Repository}'

我必须稍微更改控制器,但它现在可以正常工作。

I had to change my controller slightly but it's working now.

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

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