Zend Framework多个数据库

时间:2010-05-30 14:48:21

标签: php mysql zend-framework

我目前只使用Zend Framework的一个数据库,但现在我必须添加一个。

我现在正在使用此代码:

public static function setupDatabase()
{
    $config = self::$registry->configuration;
    $db = Zend_Db::factory($config->db->adapter, $config->db->toArray());
    $db->query("SET NAMES 'utf8'");
    self::$registry->database = $db;
    Zend_Db_Table::setDefaultAdapter($db);
}

为了使用ONE MORE数据库,我需要编写哪些代码;当我需要提出一些问题时,我将如何引用它。

4 个答案:

答案 0 :(得分:20)

这包含在框架中Zend_Application_Resource_Multidb

答案 1 :(得分:7)

在app.ini

中添加类似的内容
  [production]

  resources.multidb.db1.adapter = "pdo_mysql"

  resources.multidb.db1.host = "localhost"

  resources.multidb.db1.username = "webuser"

  resources.multidb.db1.password = "XXXX"

  resources.multidb.db1.dbname = "db1"



  resources.multidb.db2.adapter = "pdo_pgsql"

  resources.multidb.db2.host = "example.com"

  resources.multidb.db2.username = "dba"

  resources.multidb.db2.password = "notthatpublic"

  resources.multidb.db2.dbname = "db2"

  resources.multidb.db2.default = true

答案 2 :(得分:2)

在我的情况下,我有一个'核心'数据库,可以加载客户信息,包括客户的数据库连接信息。核心连接设置位于application.ini。下面的所有代码都在我的引导程序中。

加载'核心'连接(未在ini中设置为默认值):

$db_core = $this->getPluginResource('db')->getDbAdapter();
$db_core->setFetchMode(Zend_Db::FETCH_OBJ);
Zend_Registry::set('db_core', $db_core);

将设置从“核心”数据库加载到注册表后:

$settings = Zend_Registry::get('settings');
$db = Zend_Db::factory(
    $settings->db_adapter,
    array(
        'host' => $settings->db_host,
        'username' => $settings->db_user,
        'password' => $settings->db_pass,
        'dbname' => $settings->db_name,
    )
);
$db->setFetchMode(Zend_Db::FETCH_OBJ);
Zend_Db_Table::setDefaultAdapter($db);

答案 3 :(得分:0)

我认为它会对你有所帮助

在application.ini文件中

resources.multidb.db1.adapter = "pdo_mysql"
resources.multidb.db1.host    = "localhost"
resources.multidb.db1.username  = "root"
resources.multidb.db1.password  = ""
resources.multidb.db1.dbname   = "db1"
resources.multidb.db1.charset = "utf8"
resources.multidb.db1.driver_options.1002 = "SET NAMES utf8"
resources.multidb.db1.default = true

resources.multidb.db2.adapter = "pdo_mysql"
resources.multidb.db2.host = "localhost"
resources.multidb.db2.username = "root"
resources.multidb.db2.password = ""
resources.multidb.db2.dbname   = "db2"
resources.multidb.db2.charset = "utf8"
resources.multidb.db2.driver_options.1002 = "SET NAMES utf8"
resources.multidb.db2.default  = false

在bootstrap文件中

protected function _initDbAdaptersToRegistry()
{
    $this->bootstrap('multidb');
    $resource = $this->getPluginResource('multidb');

    $Adapter1 = $resource->getDb('db1');
    $Adapter2 = $resource->getDb('db2');     
    Zend_Registry::set('db1', $Adapter1);
    Zend_Registry::set('db2',$Adapter2);
}

http://www.tricksofit.com/2013/10/multiple-database-zend-framework

的更多说明