Zend Framework 2 multiple databases

喜夏-厌秋 提交于 2019-12-19 04:51:16

问题


How to configure Zend Framework 2 project to work with multiple databases? I have followed the answer in configure multiple databases in zf2

I created my own factory as MyProject/module/MyModuleName/src/MyModuleName/Classes/MyAdapterFactory.php. Is this a correct path to create the file?

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.


回答1:


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".. ;) )

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');

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;
}


来源:https://stackoverflow.com/questions/16492892/zend-framework-2-multiple-databases

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!