views:

78

answers:

3

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 adapter, just curious does bootstrap sets db adapter into registry too, or not?

+1  A: 

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.

Techpriester
yes you right I looked up it but somehow something is wrong when u vardump registry after and before bootstrapping db resource =)
simple
+1  A: 

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.

Gordon
as always great response, When dumping the registry object I can see that it actually it creates public var " public 'db' => object(Zend_Db_Adapter_Pdo_Mysql)".but still not getting how to get it from grobal registry, Well I just setted once more the db into Registry. for now will use it. Just wanted to use what already is there and that is why I asked this question , cheers
simple
@simple when you say, *dumping the registry object* are you refering to the registry inside bootstrap or the global one? The global registry is different from the local registry in bootstrap. You'd have to set the local instance to the global instance with `Zend_Registry::setInstance($bootstrap->getContainer());` if you'd want to make the local one into the global one or overwrite the `getContainer()` method in your bootstrap class to create the container with `Zend_Registry::getInstance()` instead of new `Zend_Registry`.
Gordon
ooo now the clouds are away =) , to be honest don't yet know why the local registry is needed(where it is used), but anyway thanks alot , now I have to look at the registry(global and local) from architectural perspective =)
simple
btw I am really working my way to implement the business requirements of current project, with a concept -"no overriding anything from library but use the plugins and other things , so in the future changing framework version not gonna be a hassle" , and is u can advice any good resources(blogs...) to get insite on the situation I will appreciate it.
simple
If you are looking for ZF blogs, try http://www.zftutorials.com/, http://www.zendcasts.com/ and basically all the blogs aggregated at http://www.planet-php.net/search/zend%20framework and also see the feeds on ZF's main page http://framework.zend.com/
Gordon
+1  A: 

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');
Rob Allen