Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the "right" Way to Provide a Zend Application With a Database Handler

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?

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.

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(....

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.

like image 257
Alan Storm Avatar asked Nov 14 '09 20:11

Alan Storm


People also ask

How do I run Zend Framework on localhost?

Make sure that you update your /etc/hosts or c:\windows\system32\drivers\etc\hosts file so that zf2-tutorial. localhost is mapped to 127.0. 0.1. The website can then be accessed using http://zf2-tutorial.localhost.

What is bootstrap in Zend?

In Zend Framework, bootstrapping is the process that loads your application. This includes, but is not limited to the Session. You can create many private methods inside the Bootstrap Class. All all method names starting with _init prefix will be executed once before the application starts.

How do you create a project in Zend Framework?

Open a terminal (in Windows, Start -> Run, and then use cmd). Navigate to a directory where you would like to start a project. Then, use the path to the appropriate script, and execute one of the following: % zf create project quickstart.


1 Answers

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.

like image 161
Bill Karwin Avatar answered Nov 15 '22 12:11

Bill Karwin