0

在敏捷

DB-Connected 服务允许 [...] 指定数据库适配器 [...]

(请参阅文档中的REST 服务教程)。

“DB-Connected”服务提供Database Settings,可以在其中分配适配器。每个适配器都“知道”要使用哪些凭据以及要连接哪个数据库。

我创建了一个“代码连接”的 REST 服务,并且想要版本V1V2使用单独的数据库。如何做到这一点?

4

1 回答 1

0

为了使用多个 DB 适配器,只需一个

  1. global.php在(用于一般设置driver和凭据)中创建适当的配置块local.php- 可以手动完成或通过 Apugility GUI(Dashboard -> Database Adapters

全局.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(
                ...
            ),
        ),
    ),
    ...
);

本地.php

return array(
    'db' => array(
        'adapters' => array(
            'DB\\myproject_v1' => array(
                'username' => 'root',
                'password' => 'pwd',
                'dbname' => 'myproject_v1',
                'host' => 'localhost',
            ),
            'DB\\myproject_v2' => array(
                ...
            ),
        ),
    ),
    ...
);
  1. 设置此适配器而不是默认值Zend\Db\Adapter\Adapter,例如使用ZfcBase\Mapper(这里是应用程序的示例,适配器在工厂中为service类设置;aservicecontroller/resource和之间的层DB mapper):

[我的项目]/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;
    }

}
于 2015-01-28T01:00:08.573 回答