2017-03-27 11 views
0

ガードを使用してカスタムオーセンティケータをセットアップし、サービスを自動配線しました。これはテストされ、MySQLの設定だけでうまく動作します。Symfony 3 - 複数のdb接続を持つEntityManager依存性注入

これで、2番目のデータベース接続(oracle)が指定されましたが、カスタムAuthenticatorクラスにEntityManagerをインジェクトするときに使用するデータベース接続がわからないため、symfonyはサービス設定で自動配線を許可しません。

依存関係注入を設定して特定のデータベース接続を使用できるようにするため、私はAutoWireを引き続き使用できます。ここで

Unable to autowire argument of type "Doctrine\ORM\EntityManager" for the service "user.security.login_form_authenticator". Multiple services exist for this class (doctrine.orm.prism_entity_manager, doctrine.orm.baan_entity_manager). 

config.yml

ここ
doctrine: 
    dbal: 
     connections: 
      prism: 
       driver: pdo_mysql 
       host:  "%database_host%" 
       port:  "%database_port%" 
       dbname: "%database_name%" 
       user:  "%database_user%" 
       password: "%database_password%" 
       charset: UTF8 
       # if using pdo_sqlite as your database driver: 
       # 1. add the path in parameters.yml 
       #  e.g. database_path: "%kernel.root_dir%/../var/data/data.sqlite" 
       # 2. Uncomment database_path in parameters.yml.dist 
       # 3. Uncomment next line: 
       #path:  "%database_path%" 
      baan: 
       driver: oci8 
       host:  "%baan_host%" 
       port:  "%baan_port%" 
       dbname: "%baan_db_name%" 
       user:  "%baan_user%" 
       password: "%baan_password%" 
       charset: AL32UTF8 

    orm: 
     default_entity_manager: prism 
     auto_generate_proxy_classes: "%kernel.debug%" 
     entity_managers: 
      auto_mapping: true 
      prism: 
       naming_strategy: doctrine.orm.naming_strategy.underscore 
       connection: prism 
       mappings: 
        UserBundle: 
         type: annotation 

      baan: 
       connection: baan 
       mappings: 
        BaanBundle: 
         type: annotation 

の私の教義の設定は、コンストラクタは私が正しくあなたの質問を理解したり場合、私は知らない私の認証クラスに

private $formFactory; 

    private $em; 

    private $router; 


    public function __construct(FormFactoryInterface $formFactory, EntityManager $em, RouterInterface $router) 
    { 
     $this->formFactory = $formFactory; 
     $this->em = $em; 
     $this->router = $router; 
    } 
+0

簡単。 autowireを使用しないでください。私は実際に(文書をチェックして)回避策があるかもしれないと思うが、なぜもっと魔法を加​​えるのだろうか。 – Cerad

答えて

1

です以下のように異なるデータベース接続に異なる構成を設定することができます。

dbal: 
    default_connection: default 
    connections: 
     default: 
      driver: pdo_mysql 
      host:  "%database_host%" 
      port:  "%database_port%" 
      dbname: "%database_name%" 
      user:  "%database_user%" 
      password: "%database_password%" 
      charset: UTF8 
      mapping_types: 
      enum: smallint 
     custom: 
      driver: pdo_mysql 
      host:  '%database_host2%' 
      port:  '%database_port2%' 
      dbname: '%database_name2%' 
      user:  '%database_user2%' 
      password: '%database_password2%' 
      charset: UTF8 
      mapping_types: 
      enum: smallint 
    orm: 
     default_entity_manager: default 
     auto_generate_proxy_classes: "%kernel.debug%" 
     entity_managers: 
      auto_mapping: true 
      default: 
       naming_strategy: doctrine.orm.naming_strategy.underscore 
       connection: default 
       mappings: 
        EntityBundle: 
         type: annotation 
         alias: DBAlias 
      custom: 
       naming_strategy: doctrine.orm.naming_strategy.underscore 
       connection: custom 
       mappings: 
        EntityBundle: 
         type: annotation 
         alias: DBAlias 

doctrine.orm.custom_entity_managerを使用してカスタムEntityManagerを渡すことができます。

+0

私はdoctrineの設定とクラスのコンストラクタを質問に追加しました。私のクラスコンストラクタで "プリズム"設定を$ emに渡すには? – PrestonDocks

+0

クラスをサービスに登録してから、引数として '@ doctrine.orm.prism_entity_manager'を渡すことができます。 –

+0

あなたのために働いているのかどうか、私に知らせてください。事前に感謝:) –

1

DoctrineのEntityManagerDecoratorは、EntityManagerInterfaceを実装し、そのコンストラクタにEntityManagerのインスタンスを受け入れることができます。

最初にクラスを各接続ごとに1回拡張します。

namespace MyBundle\Service\Database; 

use Doctrine\ORM\Decorator\EntityManagerDecorator; 

class PrismEntityManager extends EntityManagerDecorator {} 

class BaanEntityManager extends EntityManagerDecorator {} 

サービス構成では、これら2つのサービスを手動で配線する必要があります。

MyBundle\Service\Database\PrismEntityManager: 
    arguments: 
     $wrapped: '@doctrine.orm.prism_entity_manager' 

MyBundle\Service\Database\BaanEntityManager: 
    arguments: 
     $wrapped: '@doctrine.orm.baan_entity_manager' 

これらのサービスのいずれかを入力するだけで済みます。

public function __construct(FormFactoryInterface $formFactory, PrismEntityManager $em, RouterInterface $router) 
{ 
    $this->formFactory = $formFactory; 
    $this->em = $em; 
    $this->router = $router; 
} 
0

私はEntityManagerの場合と同じように、DBAL Connectionsで同じ問題が発生していると思います。私はいくつかのプロキシクラスを使ってこの問題を解決しました - この答えに記載されています: https://stackoverflow.com/a/46265170/6357312

私に教えてください。