Zend_Application-AvailableResources-Multidb.xml 3.4 KB

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