In order to use multiple DB adapters, one simply need
- to create an appropriate configuration block in the (
global.php
for the generall setting like driver
and local.php
for the credentials) -- it can be done manually or over the Apugility GUI (Dashboard -> Database Adapters
)
global.php
return array(
'db' => array(
'adapters' => array(
'DB\\myapi_v1' => array(
'driver' => 'Pdo',
'driver_options' => array(
1002 => 'SET NAMES \'UTF8\'',
),
'pdodriver' => 'mysql',
),
'DB\\myapi_v2' => array(
...
),
),
),
...
);
local.php
return array(
'db' => array(
'adapters' => array(
'DB\\myproject_v1' => array(
'username' => 'root',
'password' => 'pwd',
'dbname' => 'myproject_v1',
'host' => 'localhost',
),
'DB\\myproject_v2' => array(
...
),
),
),
...
);
- to set this adapter instead of the default
Zend\Db\Adapter\Adapter
, e.g. using ZfcBase\Mapper
(here an example from an application, where the adapter gets set in the factory for the service
class; a service
is here the layer between the controller
/resource
and the DB mapper
):
[myproject]/module/Portfolio/src/Portfolio/V1/Rest/Image/ImageServiceFactory.php
namespace Portfolio\V1\Rest\Image;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class ImageServiceFactory implements FactoryInterface {
public function createService(ServiceLocatorInterface $serviceManager) {
$mapper = new ImageMapper;
$mapper->setDbAdapter($serviceManager->get('DB\\myproject_v1'));
$mapper->setEntityPrototype($serviceManager->get('Portfolio\V1\Rest\Image\ImageEntity'));
$mapper->getHydrator()->setUnderscoreSeparatedKeys(false);
$service = new ImageService();
$service->setMapper($mapper);
return $service;
}
}