2016-07-22 63 views
0

在ZF2有可能configurate多个适配器这样的module.config.phpZF3多个数据库适配器

'db' => array(
    'adapters'=>array(
     'db1' => array(
      'driver' => 'Pdo', 
      'dsn' => 'mysql:dbname=zf2;host=localhost', 
      'driver_options' => array(
       PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'' 
      ), 
      'username' => 'zf2', 
      'password' => 'zf2test', 
     ), 
     'db2' => array(
      'driver' => 'Pdo', 
      'dsn' => 'mysql:dbname=zf1;host=localhost', 
      'driver_options' => array(
       PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'' 
      ), 
      'username' => 'zf1', 
      'password' => 'zf1test', 
     ), 
    ) 

), 

在控制器工厂,我可以通过的ServiceManager让他们:

class AlbumControllerFactory implements FactoryInterface 
{ 

public function createService(ServiceLocatorInterface $serviceLocator) 
    { 
     $albumTable = $serviceLocator->getServiceLocator()->get('Album\Model\AlbumTable'); 
     $db1Adapter = $serviceLocator->getServiceLocator()->get('db1'); 
     $db2Adapter = $serviceLocator->getServiceLocator()->get('db2'); 

     return new AlbumController($albumTable, $db1Adapter, $db2Adapter); 
    } 
} 

现在我试图在Zend Framework 3中做同样的事情 - 但是这种嵌套的阵列配置不起作用:

Fatal error: Uncaught exception 'Zend\Db\Adapter\Exception\InvalidArgumentException' with message 'createDriver expects a "driver" key to be present inside the parameters' in /var/www/USER/teckert/zf3/vendor/zendframework/zend-db/src/Adapter/Adapter.php:262 

我认为,在ZF 2 adapters键已经被处理时,将对DBAdapter试图创建驱动程序 - 但这不是在ZF 3.发生

任何提示的热烈欢迎......

的在zend-db with the adapters section的手册是不够清楚,我

编辑

根据this doc我已经添加下面的代码片段到全局配置文件:

'service_manager' => [ 
    'abstract_factories' => [ 
     \Zend\Db\Adapter\AdapterAbstractServiceFactory::class, 
    ], 
], 

虽然试图获得与$container->get('db1')的将对DBAdapter在我AlbumTableFactory我得到这个错误:

Unable to resolve service "db1 to a factory; are you certain you provided it during configuration? 

回答

1

好吧,我终于解决了这个问题。

正如@Pieter提到我需要以下内容排列在我config.php

'service_manager' => [ 
    'abstract_factories' => [ 
     \Zend\Db\Adapter\AdapterAbstractServiceFactory::class, 
    ], 
], 

此外,我不得不改变我的课和相关工厂都互相交谈的过程。

  1. AlbumControllerFactory被调用,得到的依赖AlbumTable服务($container->get(AlbumTable::class);),然后触发AlbumTableFactory
  2. AlbumTableFactory则准备构造函数注入了AlbumTable$tableGateway = $container->get(AlbumTableGateway::class);
  3. 现在我结合从AlbumTableFactory逻辑和将AlbumTableGatewayFactory合并为一个AbstractFactoryInterface(我完全删除了AlbumTableGatewayFactory)
// AlbumTableFactory implements AbstractFactoryInterface 

public function __invoke(ContainerInterface $container, $requestedName, array $options = null) 
{ 
    $dbAdapter = $container->get('db1'); 
    $resultSetPrototype = new ResultSet(); 
    $resultSetPrototype->setArrayObjectPrototype(new Album()); 

    $tableGateway = new TableGateway('album', $dbAdapter, null, $resultSetPrototype); 

    return new AlbumTable($tableGateway); 
} 
1

请确认您已加入Zend\Db\Adapter\AdapterAbstractServiceFactoryabstract_factories阵列的ServiceManager配置。 该抽象工厂负责实例化各个数据库适配器。另外,请检查您的Album\Model\AlbumTable工厂是否使用正确的名称检索数据库适配器。

+0

感谢您的回复。我编辑了我的文章 – rogaa

+0

不要忘记还必须安装'Zend \ Db'模块。 Zend Framework 3默认不包含它。 –