GenericDb

Name

GenericDb -- Serialize objects in DBMS

Synopsis

include("GenericDb.inc");

Description

GenericDb implements an interface for some generic database related functionality. Objects can load attributes from database, validate and update modified data and delete database object representations of themselves. It can be easily extended to specify the table and the attributes in the backend DMBS as well as the regular expressions that should match the attribute values to validate the object before saving back to database.

Source listing

      class GenericDb {
        /* give a PHPLib::db_sql object and optionally,
           the object primary key value in database table */
        function GenericDb( & $db, $ID = 0 );
	/* get and set the database table name */
	function get_table();
	function set_table($name);
 	/* get and set attribute value */
	function get($key);
	function set($key, $val);
	/* get and set a weak attribute value - these are 
           calculated and not used when inserting/updating the
	   corresponding row in the database */
	function get_w($key);
	function set_w($key, $val);
	/* get and set primary key value */
	function get_id();
	function set_id($val);
	/* get/set the primary key name */
	function get_primkey();
        function set_primkey($val);
	/* get the set(a ref to) the array of updateable column 
           names except the primary key name */
	function get_keys();
        function set_keys(& $keys);
        /* get and set array of 'weak' attribute names */
        function get_weak_keys();
        function set_weak_keys($keys);


	/* Read object from database using stored primary key 
           attribute value */
        function load();
	/* Read the single object from database, takes
	 * condition as a hash array, like array("user" => "joe",
	 * "number" => 1), compiles this into SQL query conditional part 
	 * and calls returns loadBySql
	 */
	function loadByCond ( $condition );
	/* Read object from database using given $condition
	 * which is here appended to the SQL clause, give
         * "name='joe' AND pwd='toe'", for example.
	 */
	function loadBySql( $condition );
	/* Save object to database if it is valid, if primary key value is
	 * defined, do update. Insert clause is sent to DBMS otherwise
         */
	function save();
	/* Obtains and assigns a new primary key value 
         * (the PHPLib::db_sql::nextid() way)
         * and executes SQL INSERT clause.
         */
	function Insert();
        /* Update the database representation
         */
	function Update ();
	/* Execute delete command with database
         */
	function Delete();
	/* If given attribute name, check its validity, if name not given,
	 * check validity of all attributes.
         */
	function valid($attname = "");
      }
    

If in suspect, assume the operations return false/true

Example

Example 6-1. UserDb Example

Let us have a database table user with attributes name and email. We have presented the web application user a form with two text widgets to fill in. The data is submitted and PHP creates two global variables, namely $username and $email that contain the data the user supplied. First, we have declared following:

class UserDb extends GenericDb
{
	function UserDb( $db, $ID = 0 )
	{
                $this->set_table("user");
                $keys = array(
			"name",
		        "email");
                $this->set_keys($keys);
		$this->GenericDb($db, $ID);
	}
}
      

Here we don't use regular expressions to validate database object before saving, providing only the database table name and the attributes besides the primary key name (defaults to "ID") that need updating and loading as required.

To create a blank user object and specify a My_DbSql object as means of accessing database use this syntax:

$user = new UserDb(new My_DbSql);
      

My_DbSql is derived from Db_Sql class present in PHPLib. Now, to assign values submitted by user and save the database object, we do the following:

global username,email;
$user->set("username", username);
$user->set("email", email);
$user->insert();