2017-07-02 87 views
3

我有一個包含在私有Satis存儲庫中的包,因爲它的實體和存儲庫在多個應用程序之間共享。Symfony 3.3將注入庫注入服務

使用該捆綁包的其餘應用程序是Symfony 2.7和2.8應用程序。我正在開發一個新的應用程序,並且要求使用Symfony 3.3。

在symfony的3.3應用程序,我已經在我的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 \服務\ DealerService」:方法「__construct()」的參數「$ repository」引用類「CbmLtd \ UvmsBundle \ Repository \ DealerRepository」,但不存在這樣的服務。它不能被自動註冊,因爲它來自不同的根名稱空間。

好吧,所以我想我需要顯式聲明這個存儲庫作爲服務。在Symfony 3.3文檔中找不到有關將存儲庫聲明爲服務的任何內容,2.8語法也不起作用。

我將此添加到我的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 

但我仍然得到此異常:

不能自動裝配服務 「UvmsApiV1Bundle \服務\ DealerService」:參數 「$庫」 的方法「__construct()」引用類「CbmLtd \ UvmsBundle \ Repository \ DealerRepository」,但不存在此類服務。它不能被自動註冊,因爲它來自不同的根名稱空間。

我無法對CbmLtd \ UvmsBundle進行任何更改,因爲這是由多個應用程序使用的。任何幫助,將不勝感激。我從字面上花了幾個小時,這是非常令人沮喪的。

回答

5

我能做到這一點使用以下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}' 

我不得不稍微改變我的控制器,但它的工作現在。

0

嘗試改變你的資料庫服務聲明

your_bundle.your_entity_repository: 
    class: Doctrine\ORM\EntityRepository 
    factory: ['@doctrine.orm.default_entity_manager', getRepository] 
    arguments: 
     - YourBundle\Entity\YourEntity 

,然後通過引用像這樣注入回購:

your_bundle.your_service: 
    class: YourBundle\Service\YourService 
    arguments: 
     - "@your_bundle.your_entity_repository" 
    calls: 
     - [setContainer, ['@service_container']] 
+0

得到了: 「請加入到服務的類」 car_base.app。因爲我們可能需要根據編譯時檢查來添加方法調用,所以即使它是由工廠構建的(3.4) – itnelo

0

您的服務聲明中的錯字,你忘了「資源庫「命名空間。此外,該類是位於「實體」還是「存儲庫」?服務ID應該與實際完全合格的類名相同。在services.yml,嘗試:

CbmLtd\UvmsBundle\Repository\DealerRepository: 
    class: CbmLtd\UvmsBundle\Repository\DealerRepository 

相反的:

CbmLtd\UvmsBundle\DealerRepository: 
    class: CbmLtd\UvmsBundle\Entity\DealerRepository