views:

233

answers:

1

I want one of my Zend Framework modules to use a different database from the others. The Manual suggests you can prefix the resources in application.ini with the module name to achieve this but I can't get it to work.

The relevant bits of my application.ini are:

resources.modules[] = ""

resources.db.adapter = "PDO_MYSQL"
resources.db.params.host = "localhost"
resources.db.params.dbname = "maindb"
resources.db.params.username = "dbuser"
resources.db.params.password = "..."
resources.db.params.charset = "utf8"

terms.resources.db.params.dbname = "termsdb"

where terms is the name of the module.

Is there something special I need to put in the Bootstrap to make this work?

A: 

EDIT:

In regards to the OP's first comment below...

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 Zend_Application_Resource_Db 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 :-)

So i have a MyLib_Db class that extends Zend_Db it looks something like this:

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)
   {
   }
}

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 Zend_Db_Table as well as doing some other stuff).

Then i either use MyLib_Db::getInstance()->getConnection($name); or i get the MyLib_Db instance from the bootstrap and then call getConnection.

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 Zend_Db.

Hope that helps :-)


Actually i think you need to put that in your Module's section in the config like modules.terms.resources.db.*. This should make it load in the module bootstrap. Alternatively you could manullay set it up with an _initDb in your Terms_Bootstrap.

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 $db is the manager retrieved from the resources array...

$dbAdapter = $db->getConnection('terms');

prodigitalson
The first suggestion doesn't work for me. Could you elaborate on how to set it up within the Bootstrap?
Tamlyn
@Tamlyn: See my edit above.
prodigitalson