2

在 ZF2 中,可以像这样配置多个适配器module.config.php

'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手册对我来说不够清楚

编辑

根据此文档,我已将以下代码段添加到全局配置文件中:

'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?
4

2 回答 2

2

确保您已添加Zend\Db\Adapter\AdapterAbstractServiceFactoryabstract_factoriesServiceManager 配置的数组中。这个抽象工厂负责实例化各个数据库适配器。此外,检查您的Album\Model\AlbumTable工厂是否检索到具有正确名称的数据库适配器。

于 2016-07-22T16:31:10.963 回答
1

好的,我终于解决了问题。

正如@Pieter 所提到的,我需要在我的以下数组内容config.php

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

此外,我不得不改变我的类和依赖工厂相互交谈的过程。

  1. AlbumControllerFactory被调用并获取依赖AlbumTable服务($container->get(AlbumTable::class);),然后触发AlbumTableFactory
  2. AlbumTableFactoryAlbumTable然后正在为with准备构造函数注入 $tableGateway = $container->get(AlbumTableGateway::class);
  3. 现在我将AlbumTableFactoryand the的逻辑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);
}
于 2016-07-22T21:31:11.147 回答