在 ZF2 上配置多 DB 连接 [英] Configure multi DB connections on ZF2

查看:25
本文介绍了在 ZF2 上配置多 DB 连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上是 ZF2 的初学者我设法在同一个应用程序上使用多个 BDD 并且它工作正常.(我正在谈论这个:在 zf2 中配置多个数据库).

I'm actualy a beginner in ZF2 I managed to use multiple BDD on the same application and it works. (I'm talking about this : configure multiple databases in zf2 ).

不过,我有一个小问题...

Though, I'd have a little question...

可以在 global.php 中声明我的自定义工厂吗?(在 service_manager 中).还是我需要在每个模块中声明它?(在 module.php 中)

Is it ok to declare my custom factory in global.php ? (in the service_manager thing). Or do I need to declare it inside each module ? (in module.php)

将它声明为 global.php 确实有效,但我想知道它是否没有破坏框架的精神或其他什么......

Declaring it into global.php actualy works, but I was wondering if it's not breaking the spirit of the framework or something...

感谢您的时间!

图努

推荐答案

将您的连接设置存储在本地配置中:

Store your connection settings in a local config:

config/autoload/local.php

这是为了防止您有多个具有不同数据库/连接凭据等的环境.例如,您可以提供临时设置和实时设置,两者都使用单独的数据库.然后,您也可以通过这种方式在应用程序中使用多个连接.

this is in case you have multiple environments with different databases/connection credentials etc. for example, you may gave a staging setup, and a live setup, both using a separate database. You can also then use multiple connections inside your application this way too.

没有什么可以阻止您在这里设置多个连接,并根据需要在数据库适配器等中使用它们.

there's nothing to stop you setting up multiple connections in here, and using them as needed in your database adapters etc.

local.php

return array(
    /**
     * Database Connection One
     */
    'db' => array(
        'driver'    => 'pdo',
        'dsn'       => 'mysql:dbname=dbnamehere;host=localhost',
        'username'  => 'root',
        'password'  => '',
    ),
    /**
     * Database Connection Two
     */
    'db_two' => array(
        'driver'    => 'pdo',
        'dsn'       => 'mysql:dbname=anotherdb;host=localhost',
        'username'  => 'root',
        'password'  => '',
    ),

如果您正在使用版本控制(您应该这样做!),这还允许您从存储库中排除 .local 配置文件以避免在其中存储密码等,并允许更轻松地部署到多个环境.

If you are using version control (you should be!) this also allows you to exclude the .local config files from your repository to avoid storing password etc in there, and allows for easier deployment to multiple environments.

您也可以设置多个适配器以使用不同的连接:

You can setup multiple adapters to use different connections too:

global.php

return array(
    /**
     * Database Adapter(s)
     */
    'service_manager' => array(
        'factories' => array(
            /**
             * Adapter One - this factory will use the default 'db' connection
             */
            'Zend\Db\Adapter\Adapter'   => 'Zend\Db\Adapter\AdapterServiceFactory',
            /**
             * Adapter Two - use the second connection
             */
            'Application\Db\AdapterTwo' => function($sm) {
                 $config = $sm->get('Config');
                 return new Adapter($config['db_two']);
             },
        ),
    ),
);

这篇关于在 ZF2 上配置多 DB 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆