问题描述
如何配置 Zend Framework 2 项目以使用多个数据库?我遵循了在zf2中配置多个数据库
How to configure Zend Framework 2 project to work with multiple databases?I have followed the answer in configure multiple databases in zf2
我创建了自己的工厂作为 MyProject/module/MyModuleName/src/MyModuleName/Classes/MyAdapterFactory.php.这是创建文件的正确路径吗?
I created my own factory as MyProject/module/MyModuleName/src/MyModuleName/Classes/MyAdapterFactory.php. Is this a correct path to create the file?
我不知道应该在哪里打电话:$adapter1=$serviceManager->get('myadapter1');$adapter2=$serviceManager->get('myadapter2');
I couldn't figure out where should I call:$adapter1=$serviceManager->get('myadapter1');$adapter2=$serviceManager->get('myadapter2');
而且我也不能要求更多的澄清,因为这个问题是受保护的"而且我是一个菜鸟.
And also I cant ask for more clarifications since the question is 'protected' and I'm a noob.
提前谢谢你,请拯救我的一天.
Thank you in advance and please save my day.
推荐答案
首先,更好的路径是 modules/$Module/src/$Module/Db/Adapter/MyAdapterFactory.php
, 与 命名空间 $Module\Db\Adapter
(当然不是 "$Module".. ;) )
First of all, a better path would be modules/$Module/src/$Module/Db/Adapter/MyAdapterFactory.php
, in conjuction with the namespace $Module\Db\Adapter
(of course not "$Module".. ;) )
$serviceManager->get('myadapterX')
的例子只是例子.您可以在任何可以访问 ServiceManager 的地方调用这些适配器.在控制器级别,您可以这样做:
The examples of $serviceManager->get('myadapterX')
are merely examples. Anywhere you have access to the ServiceManager you can call these adapter. On the controller level you'd do it this way:
$this->getServiceLocator()->get('myadapterX');
在定义 TableGateway 等配置级别时,它可能看起来像这样:
On configuration level when defining a TableGateway or such, it'd probably look something like this:
'my\Table\Gateway' => function ($sm) {
$dbAdapter = $sm->get('myadapterX');
$gateway = new Gateway($dbAdapter);
return $gateway;
}
这篇关于Zend Framework 2 多数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!