jump to navigation

Zend Framework : Connecting to the Database with an Reusable Database Handler April 11, 2011

Posted by Tournas Dimitrios in Zend Framework v1.10.
trackback

My previous article made an presentation how to connect to a Database on the Zend Framework . Probably , interacting with a Database is the most used functionality of every modern web-application , so a good practice would be to centralize this functionality . Of course this is not the only method available , future articles will present alternative ways .

First create a data model Class inside the model directory : application/models/Db_Db.php

php
/**
 * Database handler.
 *
 */
class Dbhandler
{

   public static function conhandler(){

      $connParams = array("host"     => "localhost",
                          "username" => "root",
                          "password" => "",
                          "dbname"   => "test");

      $db = new Zend_Db_Adapter_Pdo_Mysql($connParams);
      return $db;

   }
}

Now this resource can be accessed throughout your application with the following code :

php
require_once  APPLICATION_PATH . DIRECTORY_SEPARATOR . 'models'.DIRECTORY_SEPARATOR . 'Db_Db.php';
$db = Dbhandler::conhandler();
/**
* The connection isn't completed until you perform a query
*as the following example
*/
$statement  = "INSERT INTO accounts(
		username, email, password,status, created_date
		 )
		   VALUES(
		  'test_1', 'usermail@mail.com', 'password',
			  'active', NOW()
		   )";
 $db->query($statement);
//Close Connection
 $db->closeConnection();

As the previous code example demonstrate we had to include the path for our Database Handler with the ” require_once ”  statement . On a production application , where we have to include many resources , that would be impractical ( DRY :: Don’t Repeat Yourself ) . Zend gave us the possibility to register an AutoLoader to automate the process . Open the bootstrap.php file and add the following function :

< ? p hp 
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap 
{ 
protected function _initAutoload() 
{ 
$modelLoader = new Zend_Application_Module_Autoloader(array(
 'namespace'=>' ' ,
 'basePath' => APPLICATION_PATH)
);
 return $modelLoader;
 }

}

The front Controller  will “run” this function each time a new Zend_Application is created ( at each browser request ) . The only requirement is that our custom Classes must have a prefix of the directory where they sit’s on . Actually Zend makes a map : Models –> Model  , Forms –>Form .

<?php
/**
 * Database handler.
 *
 */
class Model_Dbhandler 
{

   public static function conhandler(){

      $connParams = array("host"     => "localhost",
                          "username" => "root",
                          "password" => "",
                          "dbname"   => "test");

      $db = new Zend_Db_Adapter_Pdo_Mysql($connParams);
      return $db;

   }

Now accessing the Class can be done without the “require_once” statement as follows :

/*
*This time no "require_once"
*/
$db = Model_Dbhandler::conhandler();
$db->getConnection();

.

Note : Zend_Application_Autoloader is like a wrapper for PHP’s include function . It takes certain value , breaks it down , and assembles it into full path . The convention is , that it takes all the underscores and replaces it with a ” slash ” , so that the final result corresponds to the directory structure of the application . And finally it adds a suffix of ” .php” . For example the value Model_Databaseconnection will be converted to APPLICATION-PATH/Models/Databaseconnection.php

Advertisements

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s