配置ZF2多个数据库(configure multiple databases in zf2)

2019-07-17 11:58发布

如何配置(和使用)的多个数据库中的Zend Framework 2? 目前我有这在我的global.php:

return array(
    'db' => array(
        'driver'         => 'Pdo',
        'dsn'            => 'mysql:dbname=my_db;host=localhost',
        'driver_options' => array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
        ),
        'username' => 'user',
        'password' => '******',
    ),
    'service_manager' => array(
        'factories' => array(
            'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
        ),
    ),
);

但我不明白的方式添加第二个。

Answer 1:

如果你看一下了Zend \ DB \适配器\ AdapterServiceFactory,你会看到你的适配器配置仅指向一个关键的'db' 。 这意味着,它建立适配器会一直使用这个(唯一的)配置项。

我建议你创建自己的工厂,应该是这样的:

namespace Your\Namespace;

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]);
  }
}

在你的主模块(或任何其他一个),添加以下到Module.php文件中声明适配器工厂了Zend服务管理器:

use Your\Namespace\MyAdapterFactory;
use Zend\ModuleManager\Feature\ServiceProviderInterface;

class Module implements ServiceProviderInterface{

//Previous code

public function getServiceConfig()
{
    return array(
        'factories' => array(
            'myadapter1'        => new MyAdapterFactory('dbconfigkey1'),
            'myadapter2'        => new MyAdapterFactory('dbconfigkey2'),
            ),
       );

}

//...

全局配置现在应该是这样的:

return array(
'dbconfigkey1' => array(
    'driver'         => 'Pdo',
    'dsn'            => 'mysql:dbname=my_db;host=localhost',
    'driver_options' => array(
        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
    ),
    'username' => 'user',
    'password' => '******',
),

'dbconfigkey2' => array(
    'driver'         => 'Pdo',
    'dsn'            => 'mysql:dbname=my_db2;host=localhost',
    'driver_options' => array(
        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
    ),
    'username' => 'user',
    'password' => '******',
),

);

使用你需要使用服务经理称他们的适配器:

$adapter1=$serviceManager->get('myadapter1');
$adapter2=$serviceManager->get('myadapter2');

2.2版

一个抽象的服务厂现在是ZF2 Zend的\ DB模块的一部分。 这是可能的“适配器”子键下添加倍数配置键:

'db'=> array(
    'adapters'=>array(
        'adapter' => array(
            'driver'         => 'Pdo',
            'dsn'            => 'mysql:dbname=test;host=localhost',
            'username' => 'readCredential',
            'password' => '****'
        ),
        'adapter2' => array(
            'driver'         => 'Pdo',
            'dsn'            => 'mysql:dbname=test;host=localhost',
            'username' => 'rwCredential',
            'password' => '****'
        ),
    )
),

然而,AbstractServiceFactory需要添加“手动”,因为它不是那么默认情况下:

'service_manager' => array(
    'abstract_factories' => array(
            'Zend\Db\Adapter\AdapterAbstractServiceFactory',
    )
),

该适配器可以访问如以前:

$adapter1=$serviceManager->get('adapter');
$adapter2=$serviceManager->get('adapter2');

从性能的角度来看,这第二个方法是较好的:一个对象将被(抽象工厂)实例化以(潜在地)创建不同的适配器。 而在以前的办法,创建每个配置中的一个对象。



Answer 2:

我找到了更好的交代上https://samsonasik.wordpress.com/2013/07/27/zend-framework-2-multiple-named-db-adapter-instances-using-adapters-subkey/

Zend框架2.2自带abstract_factories Zend\Db\Adapter\AdapterAbstractServiceFactory ,使我们能够配置多个命名DB适配器实例。 这是一步一步做到这一点:

  1. 注册Zend\Db\Adapter\AdapterAbstractServiceFactory在根据'service_manager'键'abstract_factories'类型。

    //config/autoload/global.php // ...配置/自动加载/ global.php 'service_manager' 的一部分=>数组( 'abstract_factories'=>数组( '的Zend \ DB \适配器\ AdapterAbstractServiceFactory',), )

  2. 下,在“DB”键配置“适配器”子项config/autoload/global.php

//config/autoload/global.php // ...配置/自动加载/ global.php的一部分

'db' => array(
    'adapters' => array(

        'db1' => array(
           'driver'         => 'Pdo',
           'dsn'             => 'mysql:dbname=zf2_staging;host=localhost',
           'driver_options'  => array(
                PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
            ),
        ),

        'db2' => array(
           'driver'         => 'Pdo',
           'dsn'             => 'mysql:dbname=zf2_test;host=localhost',
           'driver_options'  => array(
                PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
            ),
        ),
    ),
),
  1. 配置“适配器”下,在“DB”键子项config/autoload/local.php

//config/autoload/local.php

return array(
    'db' => array(
        'adapters' => array(
            'db1' => array(
                'username' => 'root',
                'password' => '',
            ),
            'db2' => array(
                'username' => 'other_user',
                'password' => 'other_user_passwd',
            ),
        ),
    ),
);
  1. 使用“DB1”或“DB2”从DB的ServiceManager呼叫转接适配器

    $ SM->获取( 'DB1');

    $ SM-> GET( 'DB2');

如果你需要得到$sm->get('Zend\Db\Adapter\Adapter')作为主适配器,“DB1”和“DB2”其他适配器特定的目的,那么你需要直接在数据库定义主适配器,这样的结构config/autoload/global.php将是这样的:

//config/autoload/global.php

return array(
    'db' => array(
        //this is for primary adapter....
        'driver'         => 'Pdo',
        'dsn'             => 'mysql:dbname=zf21_learn;host=localhost',
        'driver_options'  => array(
             PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
         ),

        //other adapter when it needed...
        'adapters' => array(

            'db1' => array(
               'driver'         => 'Pdo',
               'dsn'             => 'mysql:dbname=zf2_staging;host=localhost',
               'driver_options'  => array(
                    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
                ),
            ),
            'db2' => array(
               'driver'         => 'Pdo',
               'dsn'             => 'mysql:dbname=zf2_test;host=localhost',
               'driver_options'  => array(
                    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
                ),
            ),

        ),
    ),
    'service_manager' => array(
        // for primary db adapter that called
        // by $sm->get('Zend\Db\Adapter\Adapter')
        'factories' => array(
            'Zend\Db\Adapter\Adapter'
                    => 'Zend\Db\Adapter\AdapterServiceFactory',
        ),
        // to allow other adapter to be called by
        // $sm->get('db1') or $sm->get('db2') based on the adapters config.
        'abstract_factories' => array(
            'Zend\Db\Adapter\AdapterAbstractServiceFactory',
        ),
    ),
);

config/autoload/global.local.php应配置太像下面这样:

//config/autoload/local.php

return array(
    'db' => array(
        // for primary db adapter that called
        // by $sm->get('Zend\Db\Adapter\Adapter')
        'username' => 'root',
        'password' => '',

        // to allow other adapter to be called by
        // $sm->get('db1') or $sm->get('db2') based on the adapters config.
        'adapters' => array(
            'db1' => array(
                'username' => 'root',
                'password' => '',
            ),
            'db2' => array(
                'username' => 'other_user',
                'password' => 'other_user_passwd',
            ),
        ),
    ),
);


文章来源: configure multiple databases in zf2