I've here a medium-sized intranet site that is written entirely in Zend FW. The database for that intranet is situated on another server. Now I have to extend the intranet with a few new functionality. To be able to do that I have to connect with another database on a single server (and same DBMS).

Now you ask , now: What's the easiest method to do that? Must I produce a new Zend_Config object along with a new Zend_Db_Adapter? Or must i make use of the existing one and check out using the "use otherdbname" statement for connecting inside the same session towards the new database?

Or perhaps is there a much better method of doing it?

One choice is to join up 2 database handles from inside your bootstrap.php, one for every connection. E.g.:

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

Inside your remotes (e.g.):

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

I believe this is dependent how frequently you need to switch databases. Using two different plugs will differentiate between your two databases more cleanly and could be my preference.

When you are switching databases in your single adapter you'll have actually a difficult time tracing which database is presently active - bear in mind that the database connection is probably a singleton that is handed down between modules, their remotes as well as their particular models.

The 3rd option is to make use of explicit table names throughout the application. MySQL for instance offers the db_name.table_name-syntax to deal with tables in various databases on a single server. The default database is not important by doing this and Zend_Db_Table and Zend_Db_Select support this syntax as they are.

EDIT:

I have to include that option 2 and three is only going to work in case your database user has got the appropriate access privileges on all of the databases, tables and posts you need to use. Option 1 would be the only option left, in case your database takes a different user on all of your databases.