How do I code the correct database adapter into a model class that extends
the Zend_Db_Table_Abstract?
I am using Zend_Application_Resource_Multidb in my config in
application/configs/application.ini:
-----------------------------------------
resources.multidb.db1.adapter = "pdo_mysql"
resources.multidb.db1.host = "myhost1"
resources.multidb.db1.username = "myuser6854"
resources.multidb.db1.password = "mypassword"
resources.multidb.db1.dbname = "mydb0001"
resources.multidb.db1.isDefaultTableAdapter = true
resources.multidb.db2.adapter = "pdo_mysql"
resources.multidb.db2.host = "myhost2"
resources.multidb.db2.username = "myuser5242"
resources.multidb.db2.password = "mypassword"
resources.multidb.db2.dbname = "mydb0002"
-----------------------------------------
Inside my in application/bootstrap.php, I think I am loading the database
adapters. At least the default works without a problem:
-----------------------------------------
protected function _initDB()
{
$resource = $this->getPluginResource('multidb');
$resource->init();
$db1 = $resource->getDb('db1');
$db2 = $resource->getDb('db2');
$defaultDb = $resource->getDb();
}
-----------------------------------------
Inside my model classes, I understand that the property is within the
Zend_Db_Table_Abstract:
-----------------------------------------
/**
* Zend_Db_Adapter_Abstract object.
*
* @var Zend_Db_Adapter_Abstract
*/
protected $_db;
-----------------------------------------
How do I set the $_db property to correct database adapter from inside the
model?
Inside a controller, I can do the following:
-----------------------------------------
...
$bootstrap = $this->getInvokeArg('bootstrap');
$resource = $bootstrap->getPluginResource('multidb');
$db2 = $resource->getDb('db2');
$model = new Model_MyTable($db2);
...
-----------------------------------------
However, I don't just instance Models from inside Controllers. Also, I would
like to set the correct database adapter inside the model itself, so that
anything outside the Model doesn't need to understand the workings of my
databases.
There is another problem, If I want to create join that works with two
databases (and thus two database adapters), under the current architecture,
how can I do that?
Let me say up front, I want the Zend Framework to work for me, not the other
way around. The framework needs to provide a clear and uncomplicated way of
doing something that saves me time from writing it from scratch. That said,
I am new to Zend Framework and working through my first major deliverable
with it. Since this is my first time using the Framework I expect some
misunderstanding and hiccups.
Thanks for any help or insight.