问题描述:

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.

网友答案:

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;
}
相关阅读:
Top