28

如何在 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',
        ),
    ),
);

但我看不到添加第二个的方法。

4

2 回答 2

53

如果您查看 Zend\Db\Adapter\AdapterServiceFactory,您会发现您的适配器配置只指向一个 key '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');

从性能的角度来看,第二种方法更好:将实例化一个对象(抽象工厂)以(可能)创建不同的适配器。而在之前的方法中,每个配置创建一个对象。

于 2012-12-23T16:16:23.397 回答
4

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

Zend Framework 2.2 带有Zend\Db\Adapter\AdapterAbstractServiceFactory允许我们配置多个命名数据库适配器实例的 abstract_factories。这是一步一步来做的:

  1. 在“service_manager”键下的“abstract_factories”类型中注册Zend\Db\Adapter\AdapterAbstractServiceFactory

    //config/autoload/global.php //.... config/autoload/global.php 的一部分 'service_manager' => array( 'abstract_factories' => array( 'Zend\Db\Adapter\AdapterAbstractServiceFactory', ), ),

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

//config/autoload/global.php //.... config/autoload/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”作为 ServiceManager 中的 db 适配器调用适配器

    $sm->get('db1');

    $sm->get('db2');

如果您需要获取$sm->get(‘Zend\Db\Adapter\Adapter’)主适配器,'db1'和'db2'作为特定用途的其他适配器,则需要直接在db下定义主适配器,因此配置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',
            ),
        ),
    ),
);
于 2015-10-13T12:01:57.107 回答