The idea is that your Bootstrap reads a config file and you declare config entries to describe the database adapter you want to create:
[bootstrap]
resources.db.adapter = Pdo_Mysql
resources.db.params.dbname = "mydatabase"
resources.db.params.username = "webuser"
resources.db.params.password = "XXXX"
resources.db.isDefaultTableAdapter = true
If you use the config keys following the right convention, this automatically signals the Bootstrap base class to create and initialize a Zend_Application_Resource_Db
object, and stores it in the bootstrap resource registry.
Later in your Controller, you can access the resource registry.
note: I've edited this code after testing it a bit more.
class SomeController extends Zend_Controller_Action
{
public function init()
{
$bootstrap = $this->getInvokeArg("bootstrap");
if ($bootstrap->hasPluginResource("db")) {
$dbResource = $bootstrap->getPluginResource("db");
$db = $dbResource->getDbAdapter();
}
}
}
Alternatively, you can write a custom init method in your Bootstrap class, to save an object in the default Zend_Registry:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initDb()
{
if ($this->hasPluginResource("db")) {
$dbResource = $this->getPluginResource("db");
$db = $dbResource->getDbAdapter();
Zend_Registry::set("db", $db);
}
}
}
Now you can access your db object in one step instead of three:
class SomeController extends Zend_Controller_Action
{
public function init()
{
$db = Zend_Registry::get("db");
}
}
Personally, I would use the second technique, because then I have to access the resource registry only once, in my bootstrap. In the first example I would have to copy the same block of code to all of my Controllers.