在 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?