Location: PHPKode > scripts > PhpDtObject > phpdtobject/PhpDtObject.php
<?php
/**
 * Online web application builder class set
 *
 * @package OWAB
 */
/**
 * Include database logic layer
 */
include_once( 'PhpDtObject_DB.php' );
/**
 * Class PhpDtObject
 *
 * PhpDtObject, Database table object is a class, that can be used to access specific table in database.
 * Database connection (database name, table name and authorization) is defined in a separate class named DBConn.
 * PhpDtObject uses ADOdb abstraction layer (c) 2000-2002 John Lim (hide@address.com). 
 * and supports most common databases throught that package. Download ADOdb from: http://php.weblogs.com/adodb
 *
 * Database logic is separated to PhpDtObject_DB class so that SQL queries are managed/executed there.
 * 
 *
 * @package     OWAB
 * @author      Marko Manninen <hide@address.com>
 * @copyright   Copyright © 2004, Marko Manninen  
 * @date        9.6. -2004
 * @license     http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
 * @version     0.01
 * @todo        how to handle multiple primary keys, 
 */
class PhpDtObject
{
	/**
	 * Index of table columns
	 * @access private
	 * @var array
     */  
    var $_index;
	/**
	 * Database connection object
	 * @access private
	 * @var object DBConn
     */
	var $_dbconn;
	
	/**
	 * Constructor
	 * 
	 * @param object DBConn
     */
	function PhpDtObject( $dbconn )
	{
		$this->_dbconn = $dbconn;
		$pdo = new PhpDtObject_DB( $dbconn );
		$keys = $pdo->getMetaColumns();
		foreach( $keys as $num => $key )
			$this->_index[$key] = "";
	}// END OF CONSTRUCTOR

	/**#@+
	 * PUBLIC METHOD
	 * @access public
	 */		
	/**
	 * Init class
	 *
	 * !!! if( !is_numeric( $key ) ) check is needed, because ADOdb returns both associative
	 * and numeric result set array from the table. We need only associative version !!!
     */ 
	function init( $pId )
	{		
		$pdo = new PhpDtObject_DB( $this->_dbconn );
		$params['where'] = $this->getPrimaryKey() . " = '" . $pId . "'";
		// !is_numeric is because we don't want to get array values with index numbers, key names only
		if( $obj = $pdo->getOne_DB( $params ) )
		{
		 	foreach( $obj as $key => $val )
				if( !is_numeric( $key ) ) 
					$this->_index[$key] = $val;
		}
		else
			trigger_error( "Cannot initialize object with primary key: " . $pId . ". id not found.", E_USER_WARNING );
	}// END OF METHOD init
	
    /**
	 * Get value with key name
	 * 
	 * @param string pKey
	 * @return mixed
	 */ 
	function getValue( $pKey )
	{
		if( isset( $this->_index[$pKey] ) )
			return $this->_index[$pKey];
		else
			return false;
	}// END OF METHOD getValue

    /**
	 * Set value with key name
	 * 
	 * @param string $pKey
	 * @param mixed $pVal
	 * @return mixed
	 */ 
	function setValue( $pKey, $pVal )
	{
		if( isset( $this->_index[$pKey] ) )
		{
			$this->_index[$pKey] = $pVal;
			return true;
		}
		else
		{
			trigger_error( "Column name not found from the table with key: $pKey!", E_USER_NOTICE );
			return false;
		}
	}// END OF METHOD setValue

    /**
	 * Get table column index
	 * 
	 * @return array
	 */ 
	function getIndex()
	{
		return $this->_index;
	}// END OF METHOD getIndex

    /**
	 * Get primary key. Only one primary key is accepted in PhpDtObject version 0.01
	 * 
	 * @return mixed
	 */ 	
	function getPrimaryKey()
	{
		$pdo = new PhpDtObject_DB( $this->_dbconn );
		$pkeys = $pdo->getPrimaryKeys();
		if( count( $pkeys ) > 1 )
		{
			trigger_error( "Cannot initialize PhpDtObject because of multiple primary keys found from the table!", E_USER_WARNING );
			return false;
		}
		else
			return $pkeys[0];
	}// END OF METHOD getPrimaryKey
	
	/**
	 * SAVE & UPDATE
	 *
	 * Method saves the current object. If id = 0 database class
	 * uses insert to make transaction. If id > 0, then update is used.
	 * On insert, last insert id is returned to the method caller.
	 *
	 * Parameter array is used to make detailed insert/update queries. However
	 * array is optional, because database layer class will use trasfered object to read
	 * id for the where clause.
	 * 
	 * [where]			field = value
	 * [limit]			-1 ... n
	 * [custom_query]	custom query string
	 * 
	 * @params array optional
	 * @return mixed Last insert id or true/false
     */ 
	function save( $params = array( "where"=>"", "limit"=>"1" ) )
	{
		$pdo = new PhpDtObject_DB( $this->_dbconn );
		if( !isset( $params['where'] ) || $params['where'] == "" )
		{
			$params['where'] = $this->getPrimaryKey() . " = '" . $this->_index[$this->getPrimaryKey()] . "'";
		}
		return $pdo->save( $this, $params );
	}// END OF METHOD save
	
	/**
	 * DELETE
	 * 
	 * Parameters that can be used with delete:
	 * 
	 * [where]			field = 'value'
	 * [limit]			-1 ... n
	 * [custom_query]	custom query string
	 *
	 * Where key is mandatory. Limit and custom_query are optional.
	 *
	 * @params array
	 * @return boolean
     */ 
	function delete( $params = array( "where"=>"", "limit"=>"1" ) )
	{
		$pdo = new PhpDtObject_DB( $this->_dbconn );
		if( !isset( $params['where'] ) || $params['where'] == "" )
		{
			$params['where'] = $this->getPrimaryKey() . " = '" . $this->_index[$this->getPrimaryKey()] . "'";
		}
		return $pdo->delete( $params );
	}// END OF METHOD delete

	/**
	 * GET ONE
	 * 
	 * Get one row from the database and return row as an object
	 * 
	 * If many rows are affected in query, only the first is returned
	 * from the result set.
	 *
	 * Parameter array key WHERE must be used with getOne(). Otherwise database layer will 
	 * produce a warning.
	 * 
	 * @param array
	 * @return mixed 
     */ 
	function getOne( $params = array( "where"=>"" ) )
	{
		$pdo = new PhpDtObject_DB( $this->_dbconn );
		if( $obj = $pdo->getOne_DB( $params ) )
		{
			foreach( $obj as $key => $val )
				if( !is_numeric( $key ) )
					$this->_index[$key] = $val;
				
			return $this;
		}
		else
			return false;
	}// END OF METHOD getOne
		
	/**
	 * GET MANY
	 * 
	 * Get many rows from the database and return rows as objects in array
	 * 
	 * @param array optional
	 * @return mixed 
     */ 
	function getMany( $params = array( "select"=>"*", "where"=>"1", "limit"=>"-1", "order_by"=>"", "order"=>"ASC" ) )
	{
		$pdo = new PhpDtObject_DB( $this->_dbconn );
		if( $objs = $pdo->getMany_DB( $params ) )
		{		
			$arr = array();
			foreach( $objs as $obj )
			{
				foreach( $obj as $key => $val )
					if( !is_numeric( $key ) )
						$this->_index[$key] = $val;
				//
				array_push( $arr, $this );
			}
			return $arr;
		}
		else
			return false;
	}// END OF METHOD getMany
	/**#@-*/

}// END OF CLASS
?>
Return current item: PhpDtObject