Zend_Application-AvailableResources-Multidb.xml 3.4 KB

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