什么是“右”使用数据库处理程序提供Zend应用程序的方法 [英] What is the "right" Way to Provide a Zend Application With a Database Handler

查看:210
本文介绍了什么是“右”使用数据库处理程序提供Zend应用程序的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您严格遵守 ZendApplication ,您应该在哪里为应用程序开发人员设置数据库处理程序以访问?

Assuming you're hewing closely to the conventions of a ZendApplication, where should you be setting up a database handler for application developers to access?

我知道如何设置 ZendDb 适配器。我想知道的是,在Zend框架的上下文中,开发人员应该如何实例化他们的DB处理程序,所以他们不必担心在一个请求中的多个实例化,每次提供凭据等。

I know how to setup a ZendDb adapter. What I want to know is, in the context of the Zend Framework, how should developers be instantiating their DB handlers so they don't have to worry about multiple instantiations across one request, supplying credentials each time, etc.

例如,当开发人员使用Code Igniter并需要运行任意查询时,控制器上有一个数据库处理程序。

For example, when a developer is using Code Igniter and needs to run an arbitrary query, there's a database handler on the controller.

$this->db->query(....


$ b b

这个约定的Zend对应是什么?为了清楚起见,我可以考虑使用Zend Framework提供的工具来实现这一点,我要找的是如何Zend Framework,在一般

What's the Zend equivalent of this convention? To be clear, I can think of half a dozen way to accomplish this using the tools that the Zend Framework provides. What I'm looking for is how Zend Framework, in the general case, wants you to do this.

推荐答案

这个想法是你的Bootstrap读取一个配置文件,你声明配置条目来描述您要创建的数据库适配器:

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

如果按照正确的约定使用配置键,则会自动通知Bootstrap基类创建并初始化 Zend_Application_Resource_Db 对象,并将其存储在引导资源注册表

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();
        }
    }
}

自定义init方法在你的Bootstrap类中,保存一个对象在默认的Zend_Registry:

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.

这篇关于什么是“右”使用数据库处理程序提供Zend应用程序的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆