1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect2 id="zend.application.available-resources.multidb">
4 <title>Zend_Application_Resource_Multidb</title>
7 <classname>Zend_Application_Resource_Multidb</classname> is used to initialize
8 multiple Database connections. You can use the same options as you can with
9 the <link linkend="zend.application.available-resources.db">Db Resource Plugin</link>.
10 However, for specifying a default connection, you can also use the 'default' directive.
13 <example id="zend.application.available-resources.multidb.configexample">
14 <title>Setting up multiple Db Connections</title>
17 Below is an example <acronym>INI</acronym> configuration that can be used to initialize
21 <programlisting language="ini"><![CDATA[
23 resources.multidb.db1.adapter = "pdo_mysql"
24 resources.multidb.db1.host = "localhost"
25 resources.multidb.db1.username = "webuser"
26 resources.multidb.db1.password = "XXXX"
27 resources.multidb.db1.dbname = "db1"
29 resources.multidb.db2.adapter = "pdo_pgsql"
30 resources.multidb.db2.host = "example.com"
31 resources.multidb.db2.username = "dba"
32 resources.multidb.db2.password = "notthatpublic"
33 resources.multidb.db2.dbname = "db2"
34 resources.multidb.db2.default = true
38 <example id="zend.application.available-resources.multidb.retrieveSpecificDb">
39 <title>Retrieving a specific database adapter</title>
42 When using this resource plugin you usually will want to retrieve
43 a specific database. This can be done by using the resource's
44 <methodname>getDb()</methodname>. The method
45 <methodname>getDb()</methodname> returns an instance of a class that
46 extends <classname>Zend_Db_Adapter_Abstract</classname>. If you have not
47 set a default database, an exception will be thrown when this method
48 is called without specifying a parameter.
51 <programlisting language="php"><![CDATA[
52 $resource = $bootstrap->getPluginResource('multidb');
53 $db1 = $resource->getDb('db1');
54 $db2 = $resource->getDb('db2');
55 $defaultDb = $resource->getDb();
59 <example id="zend.application.available-resources.multidb.retrieveDefaultDb">
60 <title>Retrieving the default database adapter</title>
63 Additionally, you can retrieve the default database adapter
64 by using the method <methodname>getDefaultDb()</methodname>.
65 If you have not set a default adapter, the first configured db
66 adapter will be returned. Unless you specify <constant>FALSE</constant>
67 as first parameter, then <constant>NULL</constant>
68 will be returned when no default database adapter was set.
72 Below is an example that assumes the Multidb resource plugin has been configured
73 with the <acronym>INI</acronym> sample above:
76 <programlisting language="php"><![CDATA[
77 $resource = $bootstrap->getPluginResource('multidb');
78 $db2 = $resource->getDefaultDb();
80 // Same config, but now without a default db:
81 $db1 = $resource->getDefaultDb();
82 $null = $resource->getDefaultDb(false); // null