1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect2 id="zend.application.available-resources.db">
<title>Zend_Application_Resource_Db</title>
<para>
<classname>Zend_Application_Resource_Db</classname> will initialize a
<classname>Zend_Db</classname> adapter based on the options passed to it. By
default, it also sets the adapter as the default adapter for use with
<classname>Zend_Db_Table</classname>. If you want to use mutliple databases
simultaneously, you can use the <link
linkend="zend.application.available-resources.multidb">Multidb Resource
Plugin</link>.
</para>
<para>
The following configuration keys are recognized:
</para>
<itemizedlist>
<listitem>
<para>
<emphasis><property>adapter</property></emphasis>: <classname>Zend_Db</classname>
adapter type.
</para>
</listitem>
<listitem>
<para>
<emphasis><property>params</property></emphasis>: associative array of configuration
parameters to use when retrieving the adapter instance.
</para>
</listitem>
<listitem>
<para>
<emphasis><property>isDefaultTableAdapter</property></emphasis>: whether or not to
establish this adapter as the default table adapter.
</para>
</listitem>
<listitem>
<para>
<emphasis><property>defaultMetadataCache</property></emphasis>: the name of the
cache template or an instance of <classname>Zend_Cache_Core</classname> to use as
metadata cache for <classname>Zend_Db_Table</classname>.
</para>
</listitem>
</itemizedlist>
<example id="zend.application.available-resources.db.configExample">
<title>Sample DB adapter resource configuration</title>
<para>
Below is an example <acronym>INI</acronym> configuration that can be used to initialize
the DB resource.
</para>
<programlisting language="ini"><![CDATA[
[production]
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "webuser"
resources.db.params.password = "XXXXXXX"
resources.db.params.dbname = "test"
resources.db.isDefaultTableAdapter = true
; Optionally you can also the cache template to use for metadata caching:
resources.db.defaultMetadataCache = "database"
]]></programlisting>
</example>
<note>
<title>Retrieving the Adapter instance</title>
<para>
If you choose not to make the adapter instantiated with this
resource the default table adapter, how do you retrieve the adapter
instance?
</para>
<para>
As with any resource plugin, you can fetch the DB resource plugin
from your bootstrap:
</para>
<programlisting language="php"><![CDATA[
$resource = $bootstrap->getPluginResource('db');
]]></programlisting>
<para>
Once you have the resource object, you can fetch the DB adapter
using the <methodname>getDbAdapter()</methodname> method:
</para>
<programlisting language="php"><![CDATA[
$db = $resource->getDbAdapter();
]]></programlisting>
</note>
</sect2>
|