/** * @name XMLDBObject.inc.php * @author Branden Timm * @version 0.1 */ require_once 'MDB2.php'; //Note: fill in your own database information! $dsn = "driver://username:password@localhost/dbname"; $options = array( 'debug' => 2, 'portability' => MDB2_PORTABILITY_ALL, ); $db =& MDB2::connect($dsn, $options); if(PEAR::isError($db)) { die($db->getMessage()); } $db->setFetchMode(MDB2_FETCHMODE_ASSOC); // Setup a default error handler PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'errhndl'); function errhndl ($err) { echo '
' . $err->message;
    print_r($err);
    die();
} 

error_reporting(E_ALL - E_NOTICE);

abstract class DBObject	{
	protected $id = 0;
  	protected $table;
  	protected $fields = array();
  	
  	function __construct( $table, $fields )	{
    	$this->table = $table;
    	foreach( $fields as $key )
      		$this->fields[ $key ] = null;
  	}

  	function __get( $key )	{
    		return $this->fields[ $key ];
  	}

  	function __set( $key, $value )	{
    		if ( array_key_exists( $key, $this->fields ) )	{
      			$this->fields[ $key ] = $value;
      			return true;
	    	}
    		return false;
  	}

  	function load( $id ) {
    		global $db;
    		$query = "SELECT * FROM ".$this->table." WHERE ".
    		$this->table."_id = ".$id;
	    	$res =& $db->query($query);
    		
	    	if(PEAR::isError($res)) {
    			die($res->getMessage());
	    	}
    	
    		$row = $res->fetchRow(MDB2_FETCHMODE_ASSOC );
	    	if(PEAR::isError($row)) {
	    		die($row->getMessage());
    		}
    		$this->id = $id;
    		foreach( array_keys( $row ) as $key )
      			$this->fields[ $key ] = $row[ $key ];
  	}

  	function insert()  {
    		global $db;
	
    		$fields = $this->table."_id, ";
    		$fields .= join( ", ", array_keys( $this->fields ) );

	   	$inspoints = array( "0" );
    		foreach( array_keys( $this->fields ) as $field )
      		$inspoints []= "?";
    		$inspt = join( ", ", $inspoints );
			
		$sql = "INSERT INTO ".$this->table. 
   		" ( $fields ) VALUES ( $inspt )";
		
    		$values = array();
    		foreach( array_keys( $this->fields ) as $field )
      			$values []= $this->fields[ $field ];
		
    		$sth = $db->prepare( $sql );
		$sth->execute($values);
		
    		$res = $db->query( "SELECT last_insert_id()" );
    		if(PEAR::isError($res)) {
        		die($res->getMessage());
    		}	
    		$row = $res->fetchRow();
    		$this->id = $row['last_insert_id()'];
    		return $row['last_insert_id()'];
  	}	

  	function update()	{
    		global $db;

    		$sets = array();
    		$values = array();
    		foreach( array_keys( $this->fields ) as $field )  {
      			$sets []= $field.'=?';
      			$values []= $this->fields[ $field ];
    		}
    		$set = join( ", ", $sets );
    		$values []= $this->id;

		$sql = 'UPDATE '.$this->table.' SET '.$set.
  			' WHERE '.$this->table.'_id=?';

	   	$sth = $db->prepare( $sql );
	    	$sth->execute($values);
  	}

  	function delete()	{
    		global $db;
    		$sth = $db->prepare(
			'DELETE FROM '.$this->table.' WHERE '.
			$this->table.'_id=?'
    		;
    		$sth->execute(array($this->id));
  	}

  	function delete_all()	{
    		global $db;
    		$sth = $db->prepare( 'DELETE FROM '.$this->table );
    		$sth->execute();
  	}
}

abstract class XMLDBObject extends DBObject {
	function __construct($table, $fields) {
		parent::__construct($table, $fields);
	}
	
	function getXML($key) {
		$returnString = "<".$key.">".$this->fields[$key]."</".$key.">";
		return $returnString;
	}
	function asXML() {
		$returnString = "<".$this->table." id=\"".$this->id."\">";
		foreach(array_keys($this->fields) as $field) {
			if($field != $this->table."_id") {
				$returnString .= "<".$field.">".$this->fields[$field]."";
			}
		}
		$returnString .= "table.">";
		return $returnString;
	}
}

//Here is an example of a class that extends XMLDBObject
//You just need to pass it the name of the table and an array of your database fields

class Artist extends XMLDBObject {
	function __construct() {
		parent::__construct("artist",
			array("artist_name", "artist_birthday", "artist_death"));
	}
}