Hi,
I'm in the situation where I need to access tables in different databases
and looking for a "good" way of using Zend_Db with multiple databases.
Some background, I separated the public and admin area in my project and use
2 databases (admin and public) to reflect that separation. In both databases
the same menu and menu_item tables exist. Since the model to handle the
logic is also exactly the same, I want to re-use the model and database
classes I created. I set the default Zend_Db_Adapter for all tables in my
bootstrap file to point to the private database.
To process the administrative menu I use a frontcontroller plugin that calls
MenuController->processAction() to create the menu for the admin part of my
project. I also need to edit the menu of the public area via my
administration area and MenuController. What it comes down to is that I need
to process the administrative menu in one database through the same
controller as the public menus in a different database. That's where I run
into trouble. I figured I could pass in different db adapters to either
pointed to database 1 or 2, for instance MenuController->editAction() could
look like this (I've searched the list and spotted several options,
including the one Bill Karwin put forward to use Zend_Registry):
$menuTable = new MenuTable(Zend_Registry::get('public_db'));
$this->view->menu = $menuTable->fetchMenu('example');
As there is a relationship in the database between menu and menu_items, I
(somehow) also need to pass the adapter to MenuItemTable. How to do this
properly? I tried setting Zend_Db_Table::setDefaultAdapter when I needed to
switch to a different database, but that sets the adapter globally for all
tables which causes other problems when performing postDispatch database
queries as the database points to public.
I can't seem to find a solution by myself, maybe you can help me out?
Regards, TJ.