jump to navigation

Zend Framework : The Zend_Db component April 9, 2011

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

The Zend Framework is equipped with an component , the Zend_Db , that act as an interface to integrate a database into a web-application . This component and its related classes provides a  flexible , powerful but most importantly simple (easy to use) interface to connect your PHP application to an RDBMS .

Zend_Db_Adapter_Abstract  is the basic class  , actually this class is extended to create adapter classes like Zend_Db_Adapter_Pdo_Mysql , and these adapters are used  to connect  to an RDBMS . To be more precisely the Abstract Class does not only provide a function to connect to an Database but also a suite of functions to manipulate an Database ( delete , update , select …….  ) . There is a different adapter class for each brand of RDBMS . The Zend_Db abstraction is mostly built upon PHP’s PDO extension which supports a wide range of databases , so take care that this extension is installed on your PHP enabled server . Use the phpinfo()  method to verify that your server supports PDO  . The following list presents the most used databases that Zend can interact with :

There are two ways  to connect to a RDBMS with Zend_db , it’s the developers choice to select the most appropriate .

  1. Using an Adapter Class : We must instantiate a Adaptor class with the new operator .
  2. Using a Zend_Db Adapter Constructor : A factory method will instantiate the Class for us

Using an Adapter Class :

You can create an instance of an adapter using its constructor. An adapter constructor takes one argument, which is an array of parameters used to declare the connection.

public function testConnAction()
    {
	   try{
$connParams = array("host"  => "localhost",
		        "username" => "root",
		         "password" => "",
		         "dbname"   => "test");
  $db = new Zend_Db_Adapter_Pdo_Mysql($connParams);
   }catch(Zend_Db_Exception $e){
	  echo $e->getMessage();
	   }
   echo "Database object created.";
}

Using a Zend_Db Adapter Constructor :

As an alternative to using an adapter constructor directly, you can create an instance of an adapter using the static method Zend_Db::factory(). This method dynamically loads the adapter class file on demand using Zend_Loader::loadClass().

The first argument is a string that names the base name of the adapter class. For example the string ‘Pdo_Mysql’ corresponds to the class Zend_Db_Adapter_Pdo_Mysql. The second argument is the same array of parameters you would have given to the adapter constructor.

public function testConnAction()
    {
	   try{
$connParams = array("host"  => "localhost",
		       "username" => "root",
		        "password" => "",
		        "dbname"   => "test");
  $db = Zend_Db::factory('Pdo_Mysql', $connParams );
   }catch(Zend_Db_Exception $e){
	  echo $e->getMessage();
	   }
   echo "Database object created.";
}
//arcGP-74 (just a note for the author )

Alternatively , you may specify a Zend_config component as an argument in the Zend_Db::factory method . My previous article made a good presentation for the  Zend_Config components . Visit the official website to discover how to implement this alternative method .

In the examples of this article we use Zend_Db>.
Zend_Db does not actually make a connection to the database until you perform a query or call the getConnection() method on the adapter.

The Zend_Db component opens functionality that allows you to insert , delete , fetch , update and more … more …. see this link for all internal functions .

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