简体   繁体   English

使用默认资源时,Zend Db Adapter是否在注册表中注册?

[英]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 在ini文件中进行配置时,Zend Framework中有现成的资源

resource.db.user ="xxxx"...

the bootstrap sets up db adapter, just curious does bootstrap sets db adapter into registry too, or not? 引导程序设置数据库适配器,只是很好奇引导程序也将数据库适配器设置为注册表?

As far as I know, nothing is put into Zend_Registry by default. 据我所知,默认情况下,Zend_Registry不会放入任何内容。 You have to do that yourself in one of your Bootstraps _init... methods. 您必须自己使用Bootstraps _init ...方法之一来执行此操作。

From the ZF Manual on Zend Application : ZF Zend应用手册

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. Zend_Application_Bootstrap_BootstrapAbstract为这些对象提供了本地注册表。 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. 默认情况下,使用Zend_Registry的实例,但您也可以指定所需的任何其他对象。

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. 我已经检查Zend_Application_Bootstrap_BootstrapAbstract源代码,并且该容器确实是一个新的Zend_Registry实例。 Like they state in the manual, this is a local registry and not set via setInstance() to be the global instance. 就像它们在手册中指出的那样,这是一个本地注册表,而不是通过setInstance()设置为全局实例。 So if you are refering to the global Zend_Registry you get with getInstance() , then the answer is no. 因此,如果您引用的是通过getInstance()获得的全局Zend_Registry ,那么答案是否定的。 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. registerPluginResource()似乎将资源放入数组中。 Doesn't matter for your question though. 不管您的问题如何。 The answer is still no. 答案是否定的。

You can add it to the registry yourself in your Bootstrap class: 您可以自己在Bootstrap类中将其添加到注册表中:

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: 但是,如果您只想使用Db适配器,则有多种选择:

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: 首先,Zend_Application_Resource_Db将数据库适配器设置为Zend_Db_Table的默认适配器,因此您可以使用以下方法在应用程序中的任何位置检索它:

$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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM