Location: PHPKode > projects > Monyet > classes/entitybase.php
<?php

//it is like a database wrapper for corresponding class
//saving resource with layering and giving dba a single instance

Abstract Class EntityBase {

	//array of table if you want join table
	protected $tablename;
	
	protected $primarykey;

	//array of table attributes that should be displayed for end user. please exclude the primary key
	protected $attributes;

	protected $rawfields;

	//join table condition if any
	protected $condition;

	protected $dba;

	
	protected function __construct($tablename,$primarykey,$attributes,$rawfields,$condition){
		
		//rawfields: physical fields for tablename. associative array
		//attributes: more meaningful information than rawfields. normal array
  
		$this->tablename = $tablename;
		$this->primarykey = $primarykey;
		$this->attributes = $attributes;
		$this->rawfields = $rawfields;
		$this->condition = $condition;

		$this->dba = DBAccess::getInstance();
	}

	//SET GET Operation
	
	public function getPrimaryKey(){
		return $this->getPrimaryTable().'.'.$this->primarykey;
	}

	public function __call($method,$argument){
		if($method=='select'){
		   if(sizeof($argument)==2){
				return $this->selectOffset($argument[0],$argument[1]);	
			}
			else
			if(sizeof($argument)==1){
				return $this->selectWithCondition($argument[0]);	
			}
	
			else
			if(sizeof($argument)==0){
				return $this->selectAll();	
			}
			else{
				die('invalid argument for select');
			}
		}
		else
		if($method=='selectCouple'){
		   if(sizeof($argument)==2){
				return $this->selectCoupleCondition($argument[0],$argument[1],1);	
			}
			else
			if(sizeof($argument)==3){
				return $this->selectCoupleCondition($argument[0],$argument[1],$argument[2]);	
			}
			else{
				die('invalid argument for selectCouple');
			}
		}
		
		else{
			die('method '.$method.' does\'nt exists.');
		}
	}

	public function selectAll(){
		$condition = 1;
		$tablequery = $this->getTableQuery();
		$sql = 'SELECT * FROM '.$this->getPrimaryTable().' WHERE '.$condition;
		return $this->dba->query($sql);
	}

	public function truncate(){
		$sql = 'TRUNCATE '.$this->getPrimaryTable();
		$this->dba->exec($sql);

	}
	//return array of standard object, intended to build table
	public function selectOffset($offset,$limit){
		//XML Generator need 'itemid' keyword for building a table
		$field = array_merge(array($this->getPrimaryKey().' as itemid'),$this->attributes);
		$condition = $this->condition;
		$field_query= $this->getFieldQuery($field);
		$tablequery = $this->getTableQuery();
		
		$sql = 'SELECT '.$field_query." FROM ".$tablequery." WHERE ".$condition.' LIMIT '.$offset.','.$limit;
		return $this->dba->query($sql);
	}

	//return associative array array[field1]=field2. this is not ordinary select
	public function selectCoupleCondition($attr1,$attr2,$condition){
		$attr1 = $this->getField($attr1);
		$attr2 = $this->getField($attr2);
		$table = $this->getPrimaryTable();
		$sql = 'SELECT '.$attr1.','.$attr2.' FROM '.$table.' WHERE '.$condition;
		$temp = $this->dba->query($sql);
		$array = array();
		for($i=0;$i<sizeof($temp);$i++){
			$array[$temp[$i]->$attr1]=$temp[$i]->$attr2;
		}
		return $array;
	}
	//POTENTIAL BUGS: here i change the field definition
	public function selectWithCondition($condition){
		//$field = array_merge(array($this->primarykey.' as itemid',$this->primarykey),$this->attributes);
		$field = array_merge(array($this->primarykey.' as itemid'),$this->attributes);
		$field_query= $this->getFieldQuery($field);
		$tablequery = $this->getTableQuery();
		
		$sql = 'SELECT '.$field_query." FROM ".$tablequery.' WHERE '.$this->condition.' AND '.$condition;
		return $this->dba->query($sql);
	}


	//get table total rows
	public function getTotal(){
		$table = $this->getPrimaryTable();
		$sql = 'SELECT count(*) as total FROM '.$table;  
		$temp = $this->dba->query($sql);
		$result = $temp[0]->total;
		return $result;
	}

	//should return a single standard object, id==primary key 
	public function selectByID($id){
		$condition = $this->primarykey.'=\''.$id.'\'';
		$tablequery = $this->getPrimaryTable();
		
		$sql = 'SELECT * FROM '.$tablequery.' WHERE '.$condition;
		$result = $this->dba->query($sql);

	  	if(sizeof($result)!=0){
			return $result[0];
		}
	}

	//update with associative array as input
	public function update($array,$id){
		//check existence
		$result = '';
		$table = $this->getPrimaryTable();
		$queryset='';
		if($this->isExist($id)){	
			$i=0;	  
			foreach($array as $key=>$value){
			 	if($value!='NOW()'){
						$value = '\''.$value.'\'';
				} 
				if($i==0){
					$queryset = ' SET '.$this->getField($key).'='.$value;
				}
				else{
					$queryset .= ','.$this->getField($key).'='.$value;
				}
				$i++;
			}
			$sql = 'UPDATE '.$table.''.$queryset.' WHERE '.$this->primarykey.'=\''.$id.'\''; 
			$this->dba->exec($sql);
			return true;
		}
		else{
			return false;
		}
	}

	//check existence with primary key = id
	public function isExist($id){
		$where = $this->primarykey.'=\''.$id.'\'';
		return $this->isExistByCondition($where);
	}

	public function isExistByCondition($condition){
		$table = $this->getPrimaryTable();
		$sql = 'SELECT count(*) as total FROM '.$table.' WHERE '.$condition;  
		$temp = $this->dba->query($sql);
		if($temp[0]->total>0){
			return true;
		}else{
			return false;
		}

	}

	//get an attributes single row from the table, return the specified field
	public function getHead($field){
		$table = $this->getPrimaryTable();	  
		$field = $this->getField($field);
		$sql = 'SELECT * FROM '.$table.' LIMIT 0,1';
		$temp = $this->dba->query($sql);
		if(sizeof($temp)>0){
			return $temp[0]->$field;
		}
	}

	//add an array to the corresponding table column
	public function add($array){
		$table = $this->getPrimaryTable();	  
		$addquery = $this->getAddQuery($array);
		$sql = 'INSERT INTO '.$table.' VALUES ('.$addquery.')';
		$this->dba->exec($sql);
		return true;
	}

	//remove selected id
	public function remove($id){
		$table = $this->getPrimaryTable();
		$sql = 'DELETE FROM '.$table.' WHERE '.$this->primarykey.' = \''.$id.'\'';
		$this->dba->exec($sql);
		return true;
	}
	public function removeWith($field,$key){
		$table = $this->getPrimaryTable();
		$sql = 'DELETE FROM '.$table.' WHERE '.$field.' = \''.$key.'\'';
		$this->dba->exec($sql);
		return true;
	}
	public function __destruct(){
		$this->dba = null;
	}

	//get primary persistent storage for class
	public function getPrimaryTable(){
		if(is_array($this->tablename)){
			return $this->tablename[0];
		}
		else{
			return $this->tablename;
		}	
	
	}

	//build FROM clause from table definition
	public function getTableQuery(){
		$tablequery = '';
		//build join table if parameter is an array
		if(is_array($this->tablename)){
			if(sizeof($this->tablename)>0){
				$tablequery=$this->tablename[0];
			}
			for($i=1;$i<sizeof($this->tablename);$i++){
				$tablequery .= ','.$this->tablename[$i];	
			}
		}
		else{
			$tablequery = $this->tablename;
		}
		return $tablequery;
	}

	//build SELECT query from array
	public function getFieldQuery($fieldarray){
		$field_query='';

		//do-while
		if(sizeof($fieldarray)>0){
			$field_query=$fieldarray[0];
		}
		for($i=1;$i<sizeof($fieldarray);$i++){
			$field_query .= ','.$fieldarray[$i];	
		}
		return $field_query;
	}

	public function getAddQuery($field){
		$field_query='';
	  	if(sizeof($field)>0){
			if($field[0]=='NOW()'){
				$field_query .= $field[0];	
			}else{
				$field_query= '\''.$field[0].'\'';
			}
		}
	  	for($i=1;$i<sizeof($field);$i++){
			if($field[$i]=='NOW()'){
				$field_query .= ','.$field[$i];	
			}else{		 
				$field_query .= ',\''.$field[$i].'\'';	
			}
		}
		return $field_query;
	}

	public function getField($key){
		return $this->rawfields[$key];
	}
	public function getAbsField($key){
		return $this->getPrimaryTable().'.'.$this->rawfields[$key];
	}


	//not yet usefull
	public function getProperties(){
		$r = new ReflectionClass($this);
		$array = $r->getProperties();
		$result = array();
		for($i=0;$i<sizeof($array);$i++){
			if($array[$i]->isPrivate()){
				$result[] = $array[$i]->getName();	  
			}
		}
		return $result;
	}
	
}

?>
Return current item: Monyet