Home arrow PHP arrow Page 5 - Building Object-Oriented Database Interfaces in PHP: Processing Data through Data Access Objects

Updating and deleting a row: defining the "update()" and "delete()" methods - PHP

With websites now featuring full-blown dynamic applications that link to databases, data accessing has become a critical process. Often, an object-oriented solution is wanted to manage the data access operations. This works well, except when certain statements are hard-coded in that can cause headaches when a update is required. Alejandro Gervasio explains how a new category of tools, known as database interfaces, help to solve this problem.

TABLE OF CONTENTS:
  1. Building Object-Oriented Database Interfaces in PHP: Processing Data through Data Access Objects
  2. Overview of a database interface class: defining general guidelines
  3. Source code ahead: listing the code for the "DBIGenerator" class
  4. Inserting and selecting a row: the "submit()" and "load()" methods
  5. Updating and deleting a row: defining the "update()" and "delete()" methods
  6. Factoring a database interface class: the "DBIGenerator" class into action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 53
August 10, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The next methods that need to be defined are "update()" and "delete()", in order to complete the round for packaging basic DML statements. Thus, here's the code to generate the "update()" method:

// create "update" method
$str.='function update(&$db){'."n";
// build query
$str.='$sql="UPDATE users SET ';
foreach($this->options as $option){
  $str.=($option!='id')?$option.'='$this->'.$option.'',':'';
}
// remove trailing comma
$str=preg_replace("/,$/","",$str);
$str.=' WHERE id='$this->id'";'."n";
// perform query
$str.='$db->performQuery($sql);'."n";
$str.='}'."n";

The above listed method performs an "UPDATE" statement for updating a table row, using a technique similar to that shown in previous methods. Lastly, the "delete()" method, not surprisingly removes a row from the table, like this:

// create "delete" method
$str.='function delete(&$db){'."n";
// build query
$str.='$sql="DELETE FROM users WHERE id='$this->id'";'."n";
// perform query
$str.='$db->performQuery($sql);'."n";
$str.='}'."n";

Well, at this time the four essential DML methods have been properly generated. Therefore, the next step consists of creating the DB interface class, simply by writing the generated content to a file at the location specified in the $path parameter, and using the $name argument provided to the "DBIGenerator" class:

// write contents to class file
$fp=fopen($this->path.$this->name.'.php',"w") or die('Error creating class file');
fwrite($fp,$str);
fclose($fp);

 

That's all about it. If we've passed a value of "user" as the name of the DB interface class along with "DBICLASSES/" as the path where the class will be located, a class file named "user.php" will be created within the ""DBICLASSES/" directory (assuming that you have the proper file permissions), and the name of the class will be "user" too.

Certainly, the power of DB interfaces is becoming evident by now. Since each DML statement is now located within the structure of each DB interface generated, instead of duplicating SQL queries in different sections of an application, it's much easier to use the corresponding database interface to operate with the table associated to it. It is simple and effective.

With the DB interface class residing happily in the specified location, we need to instantiate an object from the class, for executing the SQL commands against the table. These kinds of objects, known as data access objects, are generated with the "getObject()" method, defined in the following way:

function getObject(){
  // create object
  if(file_exists($this->path.$this->name.'.php')){
    require_once($this->path.$this->name.'.php');
    return new $this->name;
  }
return false;
}

The above defined method checks whether the class file exists. If it does, it includes the file and return a new object from the class. Notice the usage of the "factory pattern" here. According to the parameters passed to the class, it behaves as an "object factory" returning a new DB interface object each time the method is invoked.

Once we've finished defining our "DBIGenerator" class, it would be very convenient to show how easily it creates a specific DB interface, establishing a single database table access point. Therefore, let's jump straight into the code to see this class into action.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: