PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Zend_Application_Resource_Db

Zend_Application_Resource_Db will initialize a Zend_Db adapter based on the options passed to it. By default, it also sets the adapter as the default adapter for use with Zend_Db_Table. If you want to use mutliple databases simultaneously, you can use the Multidb Resource Plugin.

The following configuration keys are recognized:

  • adapter: Zend_Db adapter type.

  • params: associative array of configuration parameters to use when retrieving the adapter instance.

  • isDefaultTableAdapter: whether or not to establish this adapter as the default table adapter.

  • defaultMetadataCache: the name of the cache template or an instance of Zend_Cache_Core to use as metadata cache for Zend_Db_Table.

Example 32. Sample DB adapter resource configuration

Below is an example INI configuration that can be used to initialize the DB resource.

[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"

Retrieving the Adapter instance

If you choose not to make the adapter instantiated with this resource the default table adapter, how do you retrieve the adapter instance?

As with any resource plugin, you can fetch the DB resource plugin from your bootstrap:

<?php
$resource 
$bootstrap->getPluginResource('db');

Once you have the resource object, you can fetch the DB adapter using the getDbAdapter() method:

<?php
$db 
$resource->getDbAdapter();

Zend Framework