PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Zend_Application_Resource_Multidb

Zend_Application_Resource_Multidb is used to initialize multiple Database connections. You can use the same options as you can with the Db Resource Plugin. However, for specifying a default connection, you can also use the 'default' directive.

Example 45. Setting up multiple Db Connections

Below is an example INI configuration that can be used to initialize two Db Connections.

[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

Example 46. Retrieving a specific database adapter

When using this resource plugin you usually will want to retrieve a specific database. This can be done by using the resource's getDb(). The method getDb() returns an instance of a class that extends Zend_Db_Adapter_Abstract. If you have not set a default database, an exception will be thrown when this method is called without specifying a parameter.

<?php
$resource 
$bootstrap->getPluginResource('multidb');
$db1 $resource->getDb('db1');
$db2 $resource->getDb('db2');
$defaultDb $resource->getDb();

Example 47. Retrieving the default database adapter

Additionally, you can retrieve the default database adapter by using the method getDefaultDb(). If you have not set a default adapter, the first configured db adapter will be returned. Unless you specify FALSE as first parameter, then NULL will be returned when no default database adapter was set.

Below is an example that assumes the Multidb resource plugin has been configured with the INI sample above:

<?php
$resource 
$bootstrap->getPluginResource('multidb');
$db2 $resource->getDefaultDb();

// Same config, but now without a default db:
$db1 $resource->getDefaultDb();
$null $resource->getDefaultDb(false); // null

Zend Framework