Location: PHPKode > scripts > PersistentObject > persistentobject/PersistentObject.php
<?php
/** PerstistentObject
  * @Author:  Paul Campbell <hide@address.com>
  * @Company: Campbell Multimedia <www.campbell-multimedia.com>
  * @Version: 0.1beta
  * @Date:    2nd July 2003
  *
  * Intended Purpose:
  * Encapsulates the generic requirements to store an object in a DB.
  * Looked at another way:  Provides a quick and easy way to load or save 
  * a single table record into a PHP Object representation.
  *
  * Note:
  * Simply use mysql_fetch_object() will NOT produce the same effect as 
  * this utility base class.
  * 
  * Intended use:
  * ABSTRACT CLASS SHOULD BE SUBCLASSED to be used to best effect at least.
  * 1. Create a class that extends PersitentObject.
  * 2. Assign that class a db_table (or each instance via setting ->db_table;
  * 3. Code vars in the subclass to match those of the db table.  (not essential, 
  *         provided for utility.
  * 4. Complete the business logic coding for your subclass as normal, taking care
  *         that the set() method in this base class is ONLY for db field value pairs.
  * 5. Create your own SQL query to return the ID of the instance you wish to load
  * 6. Call load() and commit() at will.
  *
  * Requirements:
  * 1.  A copy of the Campbell-Multimedia DB class (included with release)
  * 2.  A MySQL server which you have access to... plus PHP obviously.
  *
  * For help/support/complaints/suggestions please email the author above.
  * 
  * LICENSE: Genreal Public License.  Please see www.gpl.org for details 
  * or attached file gpl.txt
  *
  * (c) Copyright:  Campbell Multimedia 2003
  * This copyright notice (and author name above) must remain in place through any 
  * redistribution and reuse of the code below.
  */ 
require_once( "db.php" );

class PersistentObject {
	// The MySQL table to select this object from.
	var $db_table;

	// The data encapsulation attribute
	var $__data__ = array( 'id' => -1 ); // Field => Value, Field => Value...	

	// Load the data attribute from the give instance of DB from table ->db_table
	// The data will now be an associative array
	function load( &$db, $id ) {
		$q = "SELECT * FROM $this->db_table WHERE id=$id";
		//print "<p>$q</p>";
		$db->runquery( $q );
		$result = $db->getresult();
		$this->__data__ = mysql_fetch_assoc($result);
		// Set ->properties to match data array!
		$data = (array)$this->__data__;
		foreach( $data as $key => $val ) {
			$this->set( "$key", "$val" );
		}
	}
	// Testing only
	function dump( &$db ) {
		$data = (array)$this->__data__;
		foreach( $data as $key => $val ) {
			print "<p>Key: $key, Value $val</p>";
		}
	}
	function commit( &$db ) {
		$id = $this->__data__['id'];
		$q = "SELECT id FROM $this->db_table WHERE id=$id";
		$db->runquery( $q );
		if ( $db->getnumrows() < 1 ) {
			//print "No such record.  INSERT selected";
			$this->insert( $db );
		} else {
			//print "Previous detected, UPDATE selected";
			$this->update( $db );
		}
	}
	// PRIVATE (shouldn't need to call this)
	function insert( &$db ) {
		$data = (array)$this->__data__;
		$sp = "";
		foreach( $data as $key => $val ) {
			if (  $key!="id" ) { // Do NOT insert id as -1 or whatever
				$fields.=$sp."$key";
				$values.=$sp."'$val'";
				$sp = ", ";
			}
		}
		$q = "INSERT INTO $this->db_table ( $fields ) VALUES ( $values )";
		//print $q;
		$db->runquery($q); // Error reporting?
		$this->set( "id", $db->last_insert );
	}
	// PRIVATE
	function update( &$db ) {
		$data = (array)$this->__data__;
		$sp = "";
		foreach( $data as $key => $val ) {
			$mods .= $sp."$key = '$val'";
			$sp = ", ";
		}
		$q = "UPDATE $this->db_table SET $mods WHERE id = ".$data['id'];
		$db->runquery($q); // Error reporting?
	}
	// Generic getter / setters
	// IMPORTANT!  Dont NOT use this method to set class attributes 
	// that do not match the db_table fields.  This will result in a 
	// MySQL error.  You may use the normal:
	// $anOjbect->anotherProperty = "Whatever"
	// for properties outside of the DB record needed by the runtime 
	// representation including those required in subclasses.
	function set( $key, $value ) {
		$this->__data__[$key] = $value;
		$this->$key = $value;
		// Should we auto UPDATE here? Would lower chance that concurrency in apache threads corrupting state...
	}
	// Not required, as $obj->property will be set by the above set() and load() methods
	function get( $key ) {
		return $this->__data__[$key];
	}
}
?>
Return current item: PersistentObject