11

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

现在的问题是:最好的方法是什么?我应该创建一个新的 Zend_Config 对象和一个新的 Zend_Db_Adapter 吗?或者我应该使用现有的并尝试使用“使用其他数据库名称;” 在同一会话中连接到新数据库的语句?

还是有更好的方法来做到这一点?

4

5 回答 5

11

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

$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');
    ...
}
于 2009-10-04T13:11:18.730 回答
3

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

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

第三种选择是在整个应用程序中使用显式表名。例如,MySQL 提供了db_name.table_name- 语法来寻址同一服务器上不同数据库中的表。默认数据库与这种方式无关,Zend_Db_Table并且Zend_Db_Select开箱即用地支持这种语法。

编辑:

我必须补充一点,只有当您的数据库用户对您要使用的所有数据库、表和列具有适当的访问权限时,选项 2 和 3 才有效。如果您的数据库在每个数据库上都需要不同的用户,选项 1 将是唯一剩下的选项。

于 2009-10-04T13:21:21.017 回答
3

我正在使用这个 Config.ini,你也可以使用它:

[生产]
#调试输出
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
# 包含路径
includePaths.library = APPLICATION_PATH "/../library"
# 引导
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "引导程序"
# 前端控制器
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.env = APPLICATION_ENV
# 布局
#resources.layout.layout = "布局"
#resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
# 观看次数
resources.view.encoding = "UTF-8"
resources.view.basePath = APPLICATION_PATH "/views/"
# 数据库
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "本地主机"
resources.db.params.username = "root"
资源.db.params.password = ""
resources.db.params.dbname = "世界"
resources.db.isDefaultTableAdapter = true
# 会议
resources.session.save_path = APPLICATION_PATH "/../data/session"
资源.session.remember_me_seconds = 864000
[测试:生产]
#调试输出
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
# 数据库
resources.db.params.dbname = "myproject_testing"
[开发:生产]
#调试输出
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
# 数据库
resources.db.params.dbname = "myproject_development"

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

resources.db2.adapter = "pdo_mysql"
resources.db2.params.host = "本地主机"
resources.db2.params.username = "root"
资源.db2.params.password = ""
resources.db2.params.dbname = "世界"
resources.db2.isDefaultTableAdapter = true

然后你可以在引导程序或任何你喜欢的地方加载它:)而且它也很容易

于 2009-10-04T18:55:38.390 回答
3

最好的方法之一是:

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

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
    }
}
于 2013-06-01T08:16:23.953 回答
2

从我在这里找到的内容来看,为了在 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

    ....
}
于 2014-08-05T08:28:24.980 回答