Php Zend框架多数据库

Php Zend框架多数据库,php,mysql,zend-framework,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("S

我目前只使用一个带有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);
}

为了使用更多的数据库,我需要编写什么代码;当我需要进行一些查询时,我将如何引用它。

这包括在框架中。

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

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

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

$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);

在application.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

我想这会对你有帮助

在applocation.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
在引导文件中

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);
}

更多关于

的描述请看这篇文章,了解一些想法:如果其中一个答案对你的Uffo有帮助,你应该将其标记为已接受……我不知道这一个,我将不得不研究它。谢谢
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);
}