IsZend Db Adapter registered in Registry when using a default recource?
There is a out of box resources in Zend Framework, when configures in the ini file
resource.db.user ="xxxx"...
the bootstrap sets up db ada开发者_开发技巧pter, just curious does bootstrap sets db adapter into registry too, or not?
From the ZF Manual on Zend Application:
Resource Registry
Many, if not all, of your resource methods or plugins will initialize objects, and in many cases, these objects will be needed elsewhere in your application. How can you access them?
Zend_Application_Bootstrap_BootstrapAbstract
provides a local registry for these objects. To store your objects in them, you simply return them from your resources.For maximum flexibility, this registry is referred to as a "container" internally; its only requirements are that it is an object. Resources are then registered as properties named after the resource name. By default, an instance of
Zend_Registry
is used, but you may also specify any other object you wish.
Note that they also state:
Please note that the registry and also the container is not global. This means that you need access to the bootstrap in order to fetch resources.
I've checked with the sourcecode for Zend_Application_Bootstrap_BootstrapAbstract
and the container is indeed a new Zend_Registry instance. Like they state in the manual, this is a local registry and not set via setInstance()
to be the global instance. So if you are refering to the global Zend_Registry
you get with getInstance()
, then the answer is no. The db adapter won't be in there.
Note: I am not entirely sure, the db adapter is even stored inside the local registry object, since I could not find any reference for plugins being put there. registerPluginResource()
seems to puts the resource in an array. Doesn't matter for your question though. The answer is still no.
As far as I know, nothing is put into Zend_Registry by default. You have to do that yourself in one of your Bootstraps _init... methods.
You can add it to the registry yourself in your Bootstrap class:
protected function _initAddDbToRegistry()
{
$this->bootstrap('db');
$db = $this->getResource('db');
Zend_Registry::set('db', $db);
}
However, if you just want to get at the Db adapter, then there are a number of options:
Firstly, Zend_Application_Resource_Db will set the db adapter as the default for Zend_Db_Table, so you can retrieve it anywhere in your application using:
$db = Zend_Db_Table::getDefaultAdapter();
Alternatively, you can retreive it via from the front controller. Within a controller action, you can use:
$bootstrap = $this->getInvokeArg('bootstrap');
$db = $bootstrap->->getResource('db');
or throughout the application, this will work:
$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
db = $bootstrap->->getResource('db');
精彩评论