Sys Model

<?php

abstract class Model {

	private $host;
	private $db;
	private $user;
	private $pass;		
	private $conn = null;
	private $error = "";
		
	public function __construct() {
	
		$this->host	= DB_HOST;
		$this->db		= DB_NAME;
		$this->user	= DB_USER;
		$this->pass	= DB_PWD;					
		$this->open();

	}
	
	
	private function setError($msg=""){
				$this->error=$msg;
		}
		
	function getError(){
				return $this->error;
		}
		
	
	function open(){
			try{
				//$this->conn = new PDO("mysql:host=".$this->host.";port=3307;dbname=".$this->db,$this->user,$this->pass);			
				$this->conn = new PDO("mysql:host=".$this->host.";dbname=".$this->db,$this->user,$this->pass);			
				$this->conn->setAttribute(PDO::ATTR_AUTOCOMMIT,FALSE);
				$this->conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
				$this->setError();
				return true;
			}
			catch(PDOException $ex1){
				$this->setError($ex1->getMessage());
				return false;
			}
			catch(Exception $ex2){
				$this->setError($ex2->getMessage());
				return false;
			}
		}
		
	function fetchAll($tbl){	
			try {						
				$q=$this->conn->query("SELECT * FROM $tbl");
				if ($q){
					return $q->fetchAll();;
				}
				else
				  return null;  
				}
				catch(PDOException $ex1){		
					$this->setError($ex1->getMessage());
					return null;
					}					
				catch(Exception $ex){
					$this->setError($ex->getMessage());
					return null;
					}						

			}
					
		function fetchPrepare($sql,$arr,$mode=0){			
				try {			
					  $q=$this->conn->prepare($sql);						
					  $q->execute($arr);
						if ($q){												
							if ($mode==0)
						      $q->setFetchMode(PDO::FETCH_BOTH);
							else if ($mode==1)
						      $q->setFetchMode(PDO::FETCH_ASSOC);
						  else    
						      $q->setFetchMode(PDO::FETCH_NUM);
							return $q;
						}
						else{
						  return null;  
						}
					}
					catch(PDOException $ex1){		
						$this->setError($ex1->getMessage());
						return null;
						}					
					catch(Exception $ex){
						$this->setError($ex->getMessage());
						return null;
						}						
			}
					
		
		function begin(){
				$this->conn->beginTransaction();
		}

		function commit(){
				$this->conn->commit();
		}

		function __tostring(){
			 return "model object ku";
		}
						
		function rollback(){
				$this->conn->rollback();
		}		
		
		function insert($tab,$array,$auto=true){			
			$stmt="INSERT INTO $tab ";
			$field="";
			$valuesPrepare="";
			$values=array();
			  foreach ($array as $kunci => $value) {
			    $field.="$kunci,";
			    $valuesPrepare.="?,";
			    $values[]=$value;
			  }			
			  
			  $field =substr($field,0,strlen($field)-1);			  
			  $valuesPrepare =substr($valuesPrepare,0,strlen($valuesPrepare)-1);			  
			  
			  try{
			  	if ($auto) $this->begin();
				  $q=$this->conn->prepare("$stmt($field) VALUES($valuesPrepare)");
				  $q->execute($values);
				  if ($auto) $this->commit();
				  $this->setError();
				  return true;
			  }
			  catch(PDOException $e){
			  	if ($auto)$this->rollback();
			  	$this->setError($e->getMessage());
			  	return false;
			  	}
			  catch(Exception $e1){
			  	if ($auto)$this->rollback();
			  	$this->setError($el->getMessage());
			  	return false;
			  	}			  
		}
		
		
		
		function update($tab,$array,$arrayKond,$auto=true){			
			$stmt="UPDATE $tab SET ";
			$field="";
			$fieldKond="";
			$values=array();
			$valuesKond=array();
			

			  foreach ($array as $kunci => $value) {
			    $field.="$kunci=?,";
			    $values[]=$value;
			  }			

			  foreach ($arrayKond as $kunci => $value) {
			    $fieldKond.="$kunci=? and ";
			    $valuesKond[]=$value;
			    $values[]=$value;
			  }			
			  
			  $fieldKond =substr($fieldKond,0,strlen($fieldKond)-4);			  
			  
			  $field =substr($field,0,strlen($field)-1);			  
			  
			  try{
				  if ($auto) $this->begin();
				  if (count($valuesKond)==0)
				     $q=$this->conn->prepare("$stmt $field ");
				  else{
				     $q=$this->conn->prepare("$stmt $field WHERE $fieldKond");
				    }
				    
				     
				  $q->execute($values);
				  if ($auto) $this->commit();
				  $this->setError();
				  return true;
			  }
			  catch(PDOException $e){
			  	if ($auto)$this->rollback();
			  	$this->setError($e->getMessage());
			  	return false;
			  	}
			  catch(Exception $e1){
			  	if ($auto)$this->rollback();
			  	$this->setError($el->getMessage());
			  	return false;
			  	}			  
		}		
		

		function delete($tab,$arrayKond,$auto=true){			
			$stmt="DELETE FROM $tab ";
			$fieldKond="";
			$valuesKond=array();

			  foreach ($arrayKond as $kunci =>  $value) {
			    $fieldKond.="$kunci=? and ";
			    $valuesKond[]=$value;			    
			  }			
			  
			  $fieldKond =substr($fieldKond,0,strlen($fieldKond)-4);			  
			  
			  try{
				  if ($auto) $this->begin();
				  if (count($valuesKond)==0)
				     $q=$this->conn->prepare("$stmt");
				  else{
				     $q=$this->conn->prepare("$stmt WHERE $fieldKond");
				    }
				     
				    $q->execute($valuesKond);
				  if ($auto) $this->commit();
				  $this->setError();
				  return true;
			  }
			  catch(PDOException $e){
			  	if ($auto) $this->rollback();
			  	$this->setError($e->getMessage());
			  	return false;
			  	}
			  catch(Exception $e1){
			  	if ($auto) $this->rollback();
			  	$this->setError($el->getMessage());
			  	return false;
			  	}			  
		}		
		
}

	
   
?>

0 comments:

Post a Comment

 

Copyright 2013 Download Lagu Gratis: Sys Model Template by Casino Critic | Ping!. Powered by Blogger