Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>EDIT:</strong></p> <p>In regards to the OP's first comment below...</p> <p>No I cant elaborate on setting up a modular DB resource. What i provided should work in theory i believe. If it doesnt im not sure what needs to be modified in an extension of <code>Zend_Application_Resource_Db</code> because as i eluded to i dont use that resource. I have my own custom resource that allows for multiple dbs and fetching those db connections based on unique connection names. I can however ellaborate on that :-)</p> <p>So i have a <code>MyLib_Db</code> class that extends <code>Zend_Db</code> it looks something like this:</p> <pre><code>class MyLib_Db extends Zend_Db { protected $_instance = null; protected $_connections = array(); /** * Standard Zend Framework unified constructor * @param null|array An array of options that will be passed to setOptions */ public function __construct($options = null) { } /** * Standard Zend Framework setOptions implementation * @param array $options and array of options from config * @return MyLib_Db */ public function setOptions(array $options) { } /** * Set the class instance * @param MyLib_Db * @return MyLib_Db */ public static function setInstance($instance) { } /** * Get/create the singleton instance * @return MyLib_Db */ public static function getInstance() { } /** * Get a Zend_Db adapter Instance by unique name * Searches self::$_connections for the $name param as an array key * @param String $name unique connection name * @return Zend_Db_Adpater_Abstract|null */ public function getConnection($connectionName) { } /** * Add a connection instance to the registry * Adds/creates an Zend_Db_Adapter instance to the connection registry with * the string key provided by $name. If $connection is an array|Zend_Config it * should match the format used by Zend_Db::factory as it will be passed to this * function. If $name is null then the database name will be used. * @param Zend_Db_Adapter_Abstract|Zend_Config|array The connection to register * @param string|null $name A unique name for the connection * @return MyLib_Db */ public function addConnection(Zend_Db_Adapter_Abstract $connection, $name = null) { } /** * Remove/Destroy the specified connection from the registry * @param string $name the connection name to remove * @return MyLib_Db */ public function removeConnection($name) { } } </code></pre> <p>So basically my application resource for DB creates and returns an instance of the preceeding class. During creation it creates any adapters i have set up in my configuration and registers them in this class instance with a name (it also looks for a default flag for use with <code>Zend_Db_Table</code> as well as doing some other stuff).</p> <p>Then i either use <code>MyLib_Db::getInstance()-&gt;getConnection($name);</code> or i get the <code>MyLib_Db</code> instance from the bootstrap and then call <code>getConnection</code>.</p> <p>Personally i prefer to do it this way because its not dependent on having a connection being app wide or tied to a specific module which allows for more flexible reuse. That said i actually have only used this on a couple projects because on most of my Zend projects Ive been using Doctrine instead of <code>Zend_Db</code>.</p> <p>Hope that helps :-)</p> <hr> <p>Actually i think you need to put that in your Module's section in the config like <code>modules.terms.resources.db.*</code>. This should make it load in the module bootstrap. Alternatively you could manullay set it up with an <code>_initDb</code> in your <code>Terms_Bootstrap</code>.</p> <p>Personally though i use a special DB management class and put that in my resources instead - it handles setting a single or multiple adapters. Then assiming in the following that <code>$db</code> is the manager retrieved from the resources array...</p> <p><code>$dbAdapter = $db-&gt;getConnection('terms');</code></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload