繁体   English   中英

使用Zend Framework连接到两个不同的数据库

[英]connecting to two different databases with Zend Framework

我在这里有一个中等大小的Intranet网站,完全用Zend FW编写。 Intranet的数据库位于另一台服务器上。 现在,我需要使用一些新功能来扩展Intranet。 为此,我需要连接到同一服务器(和同一DBMS)上的另一个数据库。

现在的问题是:最佳方法是什么? 我应该创建一个新的Zend_Config对象和一个新的Zend_Db_Adapter吗? 还是应该使用现有的,然后尝试使用“ use otherdbname;”? 语句以在同一会话中连接到新数据库?

还是有更好的方法呢?

一种选择是从bootstrap.php注册2个数据库句柄,每个连接一个。 例如:

$parameters = array(
                    'host'     => 'xx.xxx.xxx.xxx',
                    'username' => 'test',
                    'password' => 'test',
                    'dbname'   => 'test'
                   );
try {
    $db = Zend_Db::factory('Pdo_Mysql', $parameters);
    $db->getConnection();
} catch (Zend_Db_Adapter_Exception $e) {
    echo $e->getMessage();
    die('Could not connect to database.');
} catch (Zend_Exception $e) {
    echo $e->getMessage();
    die('Could not connect to database.');
}
Zend_Registry::set('db', $db);

$parameters = array(
                    'host'     => 'xx.xxx.xxx.xxx',
                    'username' => 'test',
                    'password' => 'test',
                    'dbname'   => 'test'
                   );
try {
    $db = Zend_Db::factory('Pdo_Mysql', $parameters);
    $db->getConnection();
} catch (Zend_Db_Adapter_Exception $e) {
    echo $e->getMessage();
    die('Could not connect to database.');
} catch (Zend_Exception $e) {
    echo $e->getMessage();
    die('Could not connect to database.');
}
Zend_Registry::set('db2', $db);

在您的控制器中(例如):

public function init()
{
     $this->db = Zend_Registry::get('db');
     $this->db2 = Zend_Registry::get('db2');
}

public function fooAction()
{
    $data = $this->db2->fetchAll('select foo from blah');
    ...
}

我认为这取决于您必须多久切换一次数据库。 使用两个不同的适配器将更清楚地区分两个数据库,这是我的偏爱。

当您在单个适配器上切换数据库时,您肯定会很难跟踪当前哪个数据库处于活动状态 -请记住,数据库连接很可能是一个单例,在模块,其控制器和各自的模型之间传递。

第三种选择是在整个应用程序中使用显式表名。 例如,MySQL提供db_name.table_name -syntax来寻址同一服务器上不同数据库中的表。 默认数据库无所谓, Zend_Db_TableZend_Db_Select支持此语法。

编辑:

我必须添加选项2和3仅在您的数据库用户对要使用的所有数据库,表和列具有适当的访问权限时才起作用。 如果您的数据库在每个数据库上需要一个不同的用户,则选项1将是剩下的唯一选项。

我正在使用此Config.ini,您也可以使用它:

[production]
#Debug output
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
# Include path
includePaths.library = APPLICATION_PATH "/../library"
# Bootstrap
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
# Front Controller
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.env = APPLICATION_ENV
# Layout
#resources.layout.layout = "layout"
#resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
# Views
resources.view.encoding = "UTF-8"
resources.view.basePath = APPLICATION_PATH "/views/"
# Database
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "root"
resources.db.params.password = ""
resources.db.params.dbname = "world"
resources.db.isDefaultTableAdapter = true
# Session
resources.session.save_path = APPLICATION_PATH "/../data/session"
resources.session.remember_me_seconds = 864000
[testing : production]
#Debug output
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
# Database
resources.db.params.dbname = "myproject_testing"
[development : production]
#Debug output
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
# Database
resources.db.params.dbname = "myproject_development"

如果您需要同时连接到另一个数据库,则可以将其用于生产,测试和开发环境,您可以将数据库的配置翻倍,例如:

resources.db2.adapter = "pdo_mysql"
resources.db2.params.host = "localhost"
resources.db2.params.username = "root"
resources.db2.params.password = ""
resources.db2.params.dbname = "world"
resources.db2.isDefaultTableAdapter = true

那么您可以将其加载到引导程序或任何您喜欢的地方:),它也很容易

最好的方法之一是:

为数据库中的任何表创建新的模型表:

class Article extends Zend_Db_Table_Abstract  
{    
    protected $_name = 'id';
    public  function __construct()  {
        $adaptor = new Zend_Db_Adapter_Pdo_Mysql(array(
            'host'     => 'localhost',
            'username' => 'username',
            'password' => 'password',
            'dbname'   => 'database'

        ));
        $this->_db = $adaptor;
        parent::__construct();
    }

    // your functions goes here
    public function add($data) {
        // any syntax
    }
}

根据我在这里找到的内容,为了在Zend应用程序中使用不同的数据库,您可以根据需要遵循以下两种可能的方法之一:

-两个数据库具有相同的主机/用户

您可以在模型中指定要用于初始化$_schema变量的数据库,如下所示:

class Customer extends Zend_Db_Table_Abstract
{
    protected $_name   = 'customer';
    protected $_schema = 'db_name';

    ....
}

-两个数据库的主机/用户不同

application.ini您必须编写两个数据库的配置,如下所示:

resources.multidb.local.adapter                 = pdo_mysql
resources.multidb.local.host                    = localhost
resources.multidb.local.username                = user
resources.multidb.local.password                = ******
resources.multidb.local.dbname                  = db_name_1
resources.multidb.local.default                 = true

resources.multidb.remote.adapter                = pdo_mysql
resources.multidb.remote.host                   = remote_host
resources.multidb.remote.username               = user
resources.multidb.remote.password               = ******
resources.multidb.remote.dbname                 = db_name_2
resources.multidb.remote.default                = false

在引导程序中添加_initDbRegistry块会将数据库添加到注册表中,因此您可以访问它们:

<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    /**
     * Add databases to the registry
     * 
     * @return void
     */
    public function _initDbRegistry()
    {
        $this->bootstrap('multidb');
        $multidb = $this->getPluginResource('multidb');
        Zend_Registry::set('db_local', $multidb->getDb('local')); //db_local is going to be the name of the local adapter
        Zend_Registry::set('db_remote', $multidb->getDb('remote')); //db_remote is going to be the name of the remote adapter
    }

}

现在,您可以指定要用于每种型号的适配器,如下所示:

class Customer extends Zend_Db_Table_Abstract
{
    protected $_name    = 'customer';
    protected $_schema  = 'db_name_1';
    protected $_adapter = 'db_local'; //Using the local adapter

    ....
}

class Product extends Zend_Db_Table_Abstract
{
    protected $_name    = 'product';
    protected $_schema  = 'db_name_2';
    protected $_adapter = 'db_remote'; //Using the remote adapter

    ....
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM