質問 > 管理機能 > 複数のDBサーバに接続したい |
管理機能
スレッド表示 | 新しいものから | 前のトピック | 次のトピック | 下へ |
投稿者 | スレッド |
---|---|
Kaoru001 |
投稿日時: 2020/9/18 15:41
対応状況: −−−
|
新米 登録日: 2017/6/12 居住地: 投稿: 2 |
複数のDBサーバに接続したい ▼テンプレート
[EC-CUBE]3.0.14 [OS] Centos7 [PHP] 7.2 [データベース] MySQL 5.6 [WEBサーバ] Apache2.4 [現象] Application.phpに 'orm.ems.options' => array( 'default' => $options, 'database2' => $options2, ), とdatabase2を記載しました。 $options2にデータベースの接続情報(connection)とDoctrineのマッピング情報(dcm.yml)の場所を指定することで複数のエンティティマネージャを利用可能となる様なのですが、 $options2に引き渡すconnection、マッピング情報の場所の書き方がわかりません。 どなたか、教えて下さい |
umebius |
投稿日時: 2020/9/18 21:41
対応状況: −−−
|
神 登録日: 2016/7/22 居住地: 投稿: 2085 |
Re: 複数のDBサーバに接続したい DoctrineServiceProviderをregisterしている場所で、に対して通常はdefaultだけですが、database2というconnectionの設定も行います。('dbs.options'オプション)
DoctrineOrmServiceProviderをregisterしている場所では、通常default用の$optionしか渡されていませんが、上にご記載いただいている通り、$options2を作って渡してやります。('orm.ems.options'オプション) $options2は、$optionsを作っている場所を真似してやれば良いと思います。 この辺りです。 https://github.com/EC-CUBE/ec-cube3/blob/72eb21a0441ae7ae1b7927acfdfc31979def76b7/src/Eccube/Application.php#L453-L481 これでそれぞれのデータベース用のentityManagerを作ることができるようになります。
|
Kaoru001 |
投稿日時: 2020/9/19 13:02
対応状況: −−−
|
新米 登録日: 2017/6/12 居住地: 投稿: 2 |
Re: 複数のDBサーバに接続したい ありがとうございます。
まずdbs.optionsに次の様に追加しました $this->register(new \Silex\Provider\DoctrineServiceProvider(), array( 'dbs.options' => array( 'default' => $this['config']['database'], 'database2' => $this['config']['database2'] ) )); 続いて$options2の定義をしました $options2 = array( 'mappings' => $ormMappings ); if (array_key_exists('metadata_cache', $cacheDrivers)) { $options['metadata_cache'] = $cacheDrivers['metadata_cache']; $options2['metadata_cache'] = $cacheDrivers['metadata_cache']; } if (array_key_exists('query_cache', $cacheDrivers)) { $options['query_cache'] = $cacheDrivers['query_cache']; $options2['query_cache'] = $cacheDrivers['query_cache']; } if (array_key_exists('result_cache', $cacheDrivers)) { $options['result_cache'] = $cacheDrivers['result_cache']; $options2['result_cache'] = $cacheDrivers['result_cache']; } if (array_key_exists('hydration_cache', $cacheDrivers)) { $options['hydration_cache'] = $cacheDrivers['hydration_cache']; $options2['hydration_cache'] = $cacheDrivers['hydration_cache']; } 最後にorm.ems.optionsを定義しました $this->register(new \Dflydev\Silex\Provider\DoctrineOrm\DoctrineOrmServiceProvider(), array( 'orm.proxies_dir' => __DIR__.'/../../app/cache/doctrine/proxies', 'orm.em.options' => $options, 'orm.ems.options' => array( 'default' => $options, 'database2' => $options2, ), 'orm.custom.functions.string' => array( 'NORMALIZE' => 'Eccube\Doctrine\ORM\Query\Normalize', ), 'orm.custom.functions.numeric' => array( 'EXTRACT' => 'Eccube\Doctrine\ORM\Query\Extract', ), )); コントローラ側では $db2 = $app['orm.ems']['database2']->getConnection(); $sql = 'SELECT name FROM dtb_xxxx WHERE id in (' . implode($buf, ',') . ')'; $res = $db2->fetchAll($sql, []); と行ないました。 検索は行えるのですが、向き先がdefaultのDBに接続をされています。 getConnectionで、既に接続済みで、getConnectionはスルーされている様で、一旦DB接続をCloseするか、強制的に再接続を行いたいのですが、何か方法はありますでしょうか?? |
スレッド表示 | 新しいものから | 前のトピック | 次のトピック | トップ |