2017-07-02 16 views
3

私は個人のSatisリポジトリ内でそのエンティティとして保持されているバンドルを持っており、リポジトリは複数のアプリケーション間で共有されています。symfony 3.3サービスにリポジトリを注入する

このバンドルを使用する残りのアプリケーションは、Symfony 2.7および2.8アプリケーションです。私は新しいアプリケーションを開発しています。要件はSymfony 3.3を使用することです。

私はservices.ymlでこれを試してみましたが、symfonyの3.3アプリケーションで

# 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を" サービスをautowireすることはできません。メソッド "__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 

しかし、私はまだ、この例外を取得:

autowireできないサービス "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を "サービスを提供するためにクラスを追加してください"。コンパイル時のチェックに基づいてメソッド呼び出しを追加する必要があるかもしれないので、それが工場によって構築されたとしても、 "partnership_repository" – itnelo

0

あなたはあなたのサービスの宣言のタイプミスを持っている、あなたは、「リポジトリを忘れてしまいました"名前空間。また、クラスは「エンティティ」または「リポジトリ」に配置されていますか?サービスIDは実際の完全修飾クラス名と同じでなければなりません。 services.ymlでは、試してみてください。

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

の代わりに:

CbmLtd\UvmsBundle\DealerRepository: 
    class: CbmLtd\UvmsBundle\Entity\DealerRepository 
関連する問題