在ZF2有可能configurate多个适配器这样的module.config.php
:ZF3多个数据库适配器
'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?
感谢您的回复。我编辑了我的文章 – rogaa
不要忘记还必须安装'Zend \ Db'模块。 Zend Framework 3默认不包含它。 –