Location: PHPKode > projects > Katropine DBmanager > dbmanager/db2mysql2pro/Table.php
<?php
/**
 * use in a class eg. Users, as: 
 *  public function __getUserById($id){
 *		$sql = "SELECT * FROM ".self::$table." WHERE id = '".$id."'";
 *		$rez = Table::__getObjectArray($sql, __CLASS__);
 *		return !empty($rez)? $rez : false;
 *	}
 *
 */
class Table{
	/****************************************************
	 * private metods
	 * **************************************************
	 */ 
	private static function instatiate($record, $className){
		$object = new $className;
		foreach($record as $attribute=>$value){
			if(self::obj_has_atribute($attribute, $object)){
				$object->$attribute = $value;
			}
		}
		return $object;
		
	}
	private static function obj_has_atribute($attribute, $object){
		$object_vars = get_object_vars($object);
		return array_key_exists($attribute, $object_vars);
	}
	/****************************************************
	 * public metods
	 * **************************************************
	 */
	
	/**
	 * Enter description here...
	 *
	 * @param string $sql
	 * @param __CLASS__ $className
	 * @return array of objects "__CLASS__"
	 */
	public static function __getObjectArray($sql, $className){
		global $link;
		$mysqlOBJ = new Mysql_Data_Handler($link, '');
		$datatable = $mysqlOBJ->__dataTable($sql);
		$object_array = array();
		if(!empty($datatable)){
			foreach ($datatable as $dt=>$v){
				$object_array[] = self::instatiate($datatable[$dt], $className);
			}
		}
		return $object_array;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param string $sql
	 * @param __CLASS__ $className
	 * @return single object "__CLASS__"
	 */
	public static function __getObject($sql, $className){
		global $link;
		$mysqlOBJ = new Mysql_Data_Handler($link, '');
		$datatable = $mysqlOBJ->__dataRow($sql);
		if(!empty($datatable)){
			return self::instatiate($datatable, $className);
		}else{
			return false;
		}
	}
}
?>
Return current item: Katropine DBmanager