2013-05-23 6 views
5

私は実際にはZF2の初心者です 同じアプリケーションで複数のBDDを使用することができました。 (私はこれについて話しています:configure multiple databases in zf2)。ZF2でマルチDB接続を設定する

けれども、私は少し疑問を持っていると思います...

はそれがglobal.phpで私のカスタム工場を宣言しても大丈夫ですか? (service_managerのもの)。 それを各モジュール内で宣言する必要はありますか? (module.phpに)global.php actualyにそれを宣言

は動作しますが、それはフレームワークか何かの精神を破壊していない場合、私は思っていた...お時間を

感謝を!ローカル設定で

Tounu

答えて

7

ストア接続設定:

config/autoload/local.php 

これはあなたがたとえば、異なるデータベース/接続の資格情報を使用して、複数の環境などを持っている場合には、あなたがステージングセットアップを与えたこと、およびライブセットアップが含まれ、両方とも別のデータベースを使用します。 このようにして、アプリケーション内で複数の接続を使用することもできます。あなたは(バージョンコントロールを使用している場合など、あなたがここで複数の接続を設定し、データベースアダプタで必要に応じてそれらの使用を停止することは何も

local.phpファイル

return array(
    /** 
    * Database Connection One 
    */ 
    'db' => array(
     'driver' => 'pdo', 
     'dsn'  => 'mysql:dbname=dbnamehere;host=localhost', 
     'username' => 'root', 
     'password' => '', 
    ), 
    /** 
    * Database Connection Two 
    */ 
    'db_two' => array(
     'driver' => 'pdo', 
     'dsn'  => 'mysql:dbname=anotherdb;host=localhost', 
     'username' => 'root', 
     'password' => '', 
    ), 

はありません

あなたはそうしなければなりません!)これはまたあなたのリポジトリから.local設定ファイルを除外してそこにパスワードなどを格納することを避け、複数の環境への配備を容易にします。

あなたはセットアップ複数のアダプタは、あまりにも異なる接続を使用することができます。

global.php

return array(
    /** 
    * Database Adapter(s) 
    */ 
    'service_manager' => array(
     'factories' => array(
      /** 
      * Adapter One - this factory will use the default 'db' connection 
      */ 
      'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory', 
      /** 
      * Adapter Two - use the second connection 
      */ 
      'Application\Db\AdapterTwo' => function($sm) { 
       $config = $sm->get('Config'); 
       return new Adapter($config['db_two']); 
      }, 
     ), 
    ), 
); 
+0

ありがとうございました! – Tounu

+0

Little edit - in global.phpクラスへのパスを追加する必要があります: 'return new Zend \ Db \ Adapter \ Adapter($ config ['db_two']);' – user2047861

2

一度に複数のデータベースを接続するには、次の手順に従います

ステップ1 :

/module/MyModule /を作成し、 access.config.iniにアクセスしてアクセスします。

ステップ2: Module.phpを作成し、以下のスクリプトで/モジュール/ MyModuleという/ディレクトリに

<?php 

    namespace MyModule; 
    use MyModule\MyAdapterFactory; 
    use Zend\ModuleManager\Feature\ServiceProviderInterface; 

    class Module implements ServiceProviderInterface{ 

public function getAutoloaderConfig() 
{  
    return array(
     'Zend\Loader\StandardAutoloader' => array(
      'namespaces' => array(        
       __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__.'/Db/Adapter/', 
      ), 
     ), 
    ); 
} 

public function getServiceConfig() 
{ 
    return array(
     'factories' => array(
      'adapter1' => new MyAdapterFactory('db_adapter1'), 
      'adapter2' => new MyAdapterFactory('db_adapter2'), 
     ),  
    ); 

} 

} 

ステップ3:

がパスにMyAdapterFactory.phpを作成します/モジュール/ MyModule/src/MyModule/Db/Adapter /に以下のスクリプトを追加します。

<?php 

    namespace MyModule; 
    use Zend\ServiceManager\FactoryInterface; 
    use Zend\ServiceManager\ServiceLocatorInterface; 
    use Zend\Db\Adapter\Adapter; 

    class MyAdapterFactory implements FactoryInterface 
    { 

     protected $configKey; 

     public function __construct($key) 
     { 
      $this->configKey = $key;  
     } 

     public function createService(ServiceLocatorInterface $serviceLocator) 
     { 
      $config = $serviceLocator->get('Config'); 
      return new Adapter($config[$this->configKey]); 
     } 
     } 

    ?> 

ステップ4:

)(あなたのgetServiceConfigで以下のスクリプトを追加します。

   'YourModule\Model\YourTable' => function($sm) { 
       $tableGateway = $sm->get('YourTableGateway'); 
       $table = new YourTable($tableGateway); 
       return $table; 
      }, 
      'YourTableGateway' => function ($sm) { 
       $adapter1 = $sm->get('adapter1');     
       $resultSetPrototype = new ResultSet(); 
       $resultSetPrototype->setArrayObjectPrototype(new YourModel()); 
       return new TableGateway('tbl_name', $adapter1, null,     $resultSetPrototype); 
      }, 

ステップ5:

は、以下のようにあなたのテーブルにアクセスするには、コントローラにメソッドを追加します。

クラスの先頭でこれを宣言しますます$ this-> yourTable保護

を;

public function getYourTable() 
{ 
    if (!$this->yourTable) { 
     $sm = $this->getServiceLocator(); 
     $this->yourTable = $sm->get('YourModule\Model\YourTable'); 
    }  
    return $this->yourTable; 
} 

次に、コントローラでこの関数(getYourTable())を使用して、選択、更新、挿入のModelメソッドを呼び出すことができます。

関連する問題