Zend_Application-AvailableResources-Multidb.xml 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.retrieveSpecificDb">
  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
  57. <!-- @TODO: Should I use 'acronym' here? - Freeaqingme -->
  58. <acronym>false</acronym> as first parameter, then <acronym>null</acronym>
  59. will be returned when no default database adapter was set.
  60. </para>
  61. <programlisting language="php"><![CDATA[
  62. $resource = $bootstrap->getPluginResource('multidb');
  63. $db1 = $resource->getDb('db1');
  64. $db2 = $resource->getDb('db2');
  65. $defaultDb = $resource->getDb();
  66. ]]></programlisting>
  67. </example>
  68. <example id="zend.application.available-resources.multidb.retrieveDefaultDb">
  69. <title>Retrieving the default database adapter</title>
  70. <para>
  71. Additionally, you can use
  72. <methodname>getDefaultDb()</methodname> to retrieve the db that
  73. was set as default. If you haven't set one, it will return the first
  74. one in the stack.
  75. </para>
  76. <para>Below is an example that assumes the Multidb resource plugin has been configured
  77. with the INI sample above:</para>
  78. <programlisting language="php"><![CDATA[
  79. $resource = $bootstrap->getPluginResource('multidb');
  80. $db2 = $resource->getDefaultDb();
  81. // Same config, but now without a default db:
  82. $db1 = $resource->getDefaultDb();
  83. $null = $resource->getDefaultDb(false); // null
  84. ]]></programlisting>
  85. </example>
  86. </sect2>