如何在Doctrine的Apigile应用程序中同时使用多个版本? [英] How to use multiple versions at the same time in an Apigility app with Doctrine?
问题描述
上下文: Apigility 驱动的应用程序基于 Zend Framework 2 。在第一个版本( V1
)中,我使用的是 ZfcBase
DbMapper
。现在,我正在使用教条2 V2 >作为ORM。
Context first: Apigility driven application based on Zend Framework 2. In the first version (V1
) I was using the ZfcBase
DbMapper
for the model layer. Now I'm implementing the V2
with Doctrine 2 as ORM.
Apigility提供了一个简单的版本切换,每个版本都可以使用自己的DB适配器:
Apigility provides an easy switching between versions and every version can use its own DB adapter:
/config/autoload/global.php
/ /config/autoload/local.php
/config/autoload/global.php
/ /config/autoload/local.php
<?php
return array(
...
'db' => array(
'adapters' => array(
'DB\\myproject_v1' => array(
// settings (driver, hostname, database, driver_options)
// credentials (username, password)
...
),
'DB\\myproject_v2' => array(
// settings (driver, hostname, database, driver_options)
// credentials (username, password)
...
),
),
),
...
);
所以,要使用另一个版本作为默认的另一个数据库,它只需要更改URL:
So, to use another version as default with another database behind it only the URL needs to be changed:
myproject.tld/my-endpoint <-- version set to default
myproject.tld/v1/my-endpoint <-- version 1
myproject.tld/v2/my-endpoint <-- version 2
我想添加Doctrine 2到我的应用程序,所以我扩展了我的 local.php
像 here 显示:
I want to add Doctrine 2 to my application, so I extended my local.php
like here shown:
<?php
return array(
...
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
// settings (host, port, dbname)
// credentials (user, password)
...
),
),
),
),
...
);
它的工作,但现在我没有灵活性在版本之间切换/使用不同的具有不同数据库的版本。我的适配器设置被教条连接配置覆盖或被忽略了。
It's working, but now I don't have the flexibility for switching between the versions / for using different versions with different databases behind them. My adapter settings are getting overridden by the doctrine connection configs or just ignored.
如何将Doctrine与Apigility的版本控制灵活性相结合?如何在Doctrine的Apigility应用程序中配置数据库连接,并且能够同时在各个版本之间切换/使用多个版本?
推荐答案
您的配置的以下部分:
[
'db'=>[
'adapters' => [
'DB\\myproject_v1' => [],
'DB\\myproject_v2' => [],
]
]
]
工厂用db适配器实例注册服务名称 DB\\myproject_v1
和 DB\\myproject_v2
。
configures an abstract factory that registers the service names DB\\myproject_v1
and DB\\myproject_v2
with db adapter instances.
下面的配置部分实际上将适配器分配给数据库连接的资源:
It is the part of the configuration below that actually assigns the adapter to your DB connected resource:
'db-connected' => array(
'YourDBConnectedResource' => array(
'adapter_name' => 'DB\\myproject_v1',
),
),
Doctrine提供了自己的抽象工厂和配置,所以要让Apigility与Doctrine一起作为数据库适配器,你只需要调整配置有点。首先,让我们添加一个第二个连接到你的doctrine配置来帮助说明这个变化。
Doctrine provides its own abstract factory and configuration, so to make Apigility work with Doctrine as your database adapter, you just have to tweak the configuration a bit. First, lets add a second connection to your doctrine config to help illustrate the change.
return [
'doctrine' => [
'connection' => [
'orm_default' => [ // you don't have to use orm_default, you can arbitrarily name this version1
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => [
// settings (host, port, dbname)
// credentials (user, password)
],
],
'version2' => [
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => [
// settings (host, port, dbname)
// credentials (user, password)
],
],
],
],
];
现在,您的数据库适配器名称为 doctrine.connection.orm_default
和 doctrine.connection.version2
。所以你替代你的数据库连接的配置块中的那些。
Now, your DB adapter names are doctrine.connection.orm_default
and doctrine.connection.version2
. So you substitute those in your db-connected configuration block.
'db-connected' => [
'My\\Endpoint\\V1\\Rest\\MyResource' => [
'adapter_name' => 'doctrine.connection.orm_default',
],
'My\\Endpoint\\V2\\Rest\\MyResource' => [
'adapter_name' => 'doctrine.connection.version2',
],
],
这篇关于如何在Doctrine的Apigile应用程序中同时使用多个版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!