问题描述
我有一个捆绑软件,该捆绑软件保存在一个私有的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}'
上面给出了以下异常:
好,所以我想我需要显式声明此存储库作为服务。我在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:
我无法对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将存储库注入服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!