Multiple databases

View: New views
2 Messages — Rating Filter:   Alert me  

Multiple databases

by monk.e.boy :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

  I have multiple databases in my application.

  Each database more or less corresponds to a controller, so in the __construct of each controller I connect to the required DB:

public function __construct(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response, array $invokeArgs = array())
{
        $config = new Zend_Config_Ini('/var/www/ZF-apps/config.ini', 'general');
        $db = Zend_Db::factory($config->keyword->db);
        Zend_Db_Table::setDefaultAdapter($db);
        // ... etc etc...

Now this works fine. But my real problem is that some of my databases have tables with the same named tables.

For example, al lot of the DBs have an 'ajax_msg' table that background tasks use to communicate with the GUI.

How would you suggest I arrange my /app/models/ directory? And name my model classes?

My initial thoughts are:

DBName_TableName == /apps/models/DBName/TableName.php  (<-- would this work?)

Looking forward to some insigntful comments :-)  ;-)  (especially about my odd DB design
;-)

monk.e.boy

Re: Multiple databases

by Bill Karwin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


monk.e.boy wrote:
DBName_TableName == /apps/models/DBName/TableName.php  (<-- would this work?)
Well, it would work in that you can declare a class that way.  But how to use it?  Since you said each controller knows its own database name, it should know how to instantiate "new DBName_TableName()".  But if you're getting the name of the DB from a config file, then you don't really know the name of the DB until runtime.

You can also load Table classes by their simple table name, if you use set_include_path() to add "/apps/models/DBName".

monk.e.boy wrote:
Looking forward to some insigntful comments :-)  ;-)  (especially about my odd DB design
;-)
I can't comment on your choice of creating multiple databases, since I don't know why you did it that way.  If you did it for master/slave failover, or for load-balancing, then that's fine.  However, if you did it so you can have a different database "per" some attribute, then that's probably a self-defeating architecture.

You might be interested in declaring Table class with a schema-qualified name:

class MyTable extends Zend_Db_Table_Abstract
{
  protected $_name = 'dbname.mytable';
}

There's also a $_schema class variable, but its implementation is spotty and it's not necessary anyway, since you can achieve the same result more reliably by specifying the schema-qualified name.

Regards,
Bill Karwin
LightInTheBox - Buy quality products at wholesale price