views:

182

answers:

3

I'm currently using only one database with Zend Framework, but now I have to add ONE MORE.

I'm using this code right now:

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

What code do I need to write in order to use ONE MORE database; and how I will reference it, when I need to make some queries or so..

Best Regards!

A: 

perhaps this post can help: Using the Zend Framework with multiple satabases

jigfox
+5  A: 

This is included in the framework as Zend_Application_Resource_Multidb.

chelmertz
I was not aware of this one, I'll have to look into it. Thanks!
Sonny
A: 

In my situation, I have a 'core' database that loads customer info, including the customer's database connection info. The core connection settings are in the application.ini. All the code below is in my bootstrap.

Loading 'core' connection (not set to default in ini):

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

After settings are loaded from 'core' database into the registry:

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