Introduction |
| I recently discovered DB_DataObject database abstraction layer class from PEAR, it's a database class that every web developer needs. |
| In this tutorial I will show you how to take advantage of the DB_DataObject class and you will notice how this class will strongly affect your developing time. |
| |
 |
Please be aware that using DB_DataObject class does not save you from learning sql, therefore you should already know how to build a query. |
|
| |
| As you will see later, in all the articles posted on AskBee.NET, you will find full working and tested examples. So let's get it started. |
| |
| How does DB_DataObject package works? It will create classes for every table in the database. |
| For example if we have a table called clients in our database, a file called Clients.php will be created. This file will contain a class called DataObjects_Clients that extends the base class DB_DataObject. |
| |
 |
WARNING! Zend Optimizer: due to a bug in the optimizer, you will have to either reduce the optimization level, or define the constant DB_DATAOBJECT_NO_OVERLOAD = 0 otherwise PHP may segfault |
|
| |
Step One - PEAR package installation |
| First of all you need to download and install PEAR packages from . |
The packages we need are:
- - PEAR Base System 1.3.6
- - DataBase Abstraction Layer 1.7.6
- - An SQL Builder, Object Interface to Database Tables 1.7.15
as DB_DataObject package is dependent on other two. You should download the latest stable version of every package.
|
| You should put all the PEAR packages in one directory, and its structure should be the same like the one in the next file . |
| |
Step Two - Create Directories Structure |
| I will use includes directory and in this directory pear directory will be created and another directory called dbdataobject. In the last directory dbdataobject classes are generated. If you are working in a unix environment make sure full permissions are set for this directory. |
| |
Step Three - Create Database Structure |
| We will create just one table first with four fields. The table name is clients and it has the strucure below. |
 |
| |
Step Four - Dynamic Class Generation |
| So as I said before, we need to generate dynamic classes for every table in our database. Therefore we need to create two files: createTables.php and dbconfig.php |
| First file createTables.php is the script that will create this tables. |
define('ABS_SERVER', 'D:/PHP/dbdataobject_article/');
ini_set('include_path', ABS_SERVER . ';' . ABS_SERVER . 'includes/pear/;' . ABS_SERVER . 'includes/');
$_SERVER['argv'][1] = "dbconfig.php";
require_once("DB/DataObject/createTables.php");
|
|
| |
| Second file dbconfig.php contains database information and location paths |
[DB_DataObject]
database = mysql://username:password@hostname/database_name
schema_location = includes/dataobject
class_location = includes/dataobject
require_prefix = DataObjects/
class_prefix = DataObjects_
|
|
Make sure you remove this two files after the website goes live!!!
After you've created this two files, type in your browser http://your_website_link/createTables.php
and then go to the class_location directory (includes/dataobject in this case) and you will find the classes created.
|
| |
 |
| |
Step Five - Simple Example |
| A simple example of getting records from clients table. |
| We create a file named startup.php where we can find the database settings. This file should look like this. |
define('ABS_SERVER', 'D:/PHP/dbdataobject_article/');
ini_set('include_path', ABS_SERVER . ';' . ABS_SERVER . 'includes/pear/;' . ABS_SERVER . 'includes/');
//ini_set('include_path', '.:./includes/pear:./includes');
include_once('DB/DataObject.php' );
include_once('DB/DataObject/Cast.php');
$options = &PEAR::getStaticProperty('DB_DataObject','options');
$options = array(
'database' => 'mysql://username:password@hostname/database_name',
'schema_location' => ABS_SERVER . 'includes/dataobject//',
'class_location' => ABS_SERVER . 'includes/dataobject/',
'require_prefix' => 'DataObjects/',
'class_prefix' => 'DataObjects_',
);
|
|
| |
| And the second file is the file that gets all the records from the database. |
require_once( 'startup.php' );
$client = DB_DataObject::factory( 'clients' );
$client->find();
while( $client->fetch() )
{
echo $client->name . ' - ' . $client->email . ' - ' . $client->job;
}
|
|
| |
| This example was tested on a Windows platform, PHP 4.3.9, MySQL 4.0.12, Apache 1.3.33 |
| Download the whole working archive |
| |