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

查看:18
本文介绍了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
    CbmLtdUvmsBundle:
        resource: '../../vendor/cbmltd/uvms-bundle/*'
        exclude: '../../vendor/cbmltd/uvms-bundle/{Entity,Repository}'

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

上面给出了以下例外:

无法自动装配服务UvmsApiV1BundleServiceDealerService":方法__construct()"的参数$repository"引用了类CbmLtdUvmsBundleRepositoryDealerRepository",但不存在此类服务.它无法自动注册,因为它来自不同的根命名空间.

Cannot autowire service "UvmsApiV1BundleServiceDealerService": argument "$repository" of method "__construct()" references class "CbmLtdUvmsBundleRepositoryDealerRepository" 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:
    ...
    CbmLtdUvmsBundleDealerRepository:
        class: CbmLtdUvmsBundleEntityDealerRepository
        factory_service: doctrine.orm.entity_manager
        factory_method: getRepository
        arguments:
            - CbmLtdUvmsBundleEntityDealer

但我仍然得到这个异常:

But I still get this exception:

无法自动装配服务UvmsApiV1BundleServiceDealerService":方法__construct()"的参数$repository"引用了类CbmLtdUvmsBundleRepositoryDealerRepository",但不存在此类服务.它无法自动注册,因为它来自不同的根命名空间.

Cannot autowire service "UvmsApiV1BundleServiceDealerService": argument "$repository" of method "__construct()" references class "CbmLtdUvmsBundleRepositoryDealerRepository" but no such service exists. It cannot be auto-registered because it is from a different root namespace.

我无法对 CbmLtdUvmsBundle 进行任何更改,因为它被多个应用程序使用.任何帮助,将不胜感激.我确实花了几个小时在这上面,这非常令人沮丧.

I cannot make any changes to CbmLtdUvmsBundle 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
    CbmLtdUvmsBundle:
        resource: '../../vendor/cbmltd/uvms-bundle/*'
        exclude: '../../vendor/cbmltd/uvms-bundle/{Entity,Repository}'

    CbmLtdUvmsBundleRepositoryDealerRepository:
        factory: doctrine.orm.entity_manager:getRepository
        arguments:
            - CbmLtdUvmsBundleEntityDealer

    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天全站免登陆