Location: PHPKode > scripts > Abstract SQL Table to Object > abstract-sql-table-to-object/parent.dbtoclass.php
<?php
/*
	Database to class interface .
	By Sergey Shilko 28.03.2007
	Rev.: 1.0
	ICQ : 201821805
*/





class dbtoclass {
    var $db;
    var $updated_vars;
    var $id;

    #requires php extension `runkit` , and dont work for now >>>
    /*
    function evalGetSetMethods($class = false){
       foreach(get_object_vars($this) as $k => $v){
            runkit_method_add($class,'set'.ucfirst($k),'$var','$this->'.$k.' = $var;',RUNKIT_ACC_PUBLIC);
            runkit_method_add($class,'get'.ucfirst($k),'','return $this->'.$k.';',RUNKIT_ACC_PUBLIC);
       }
    }
    */
    #<<<

    function makeGetSetMethods(){
       foreach(get_object_vars($this) as $k => $v){
           echo "\n\t function set".ucfirst($k)."(\$var){\r\t\t
           ".'$this->'.$k.' = $var;
           '."\r}";
           #echo "\n";
           echo "\n\t function get".ucfirst($k)."(){\r\t\t
           return ".'$this->'.$k.";
           \r\t}";
           echo "\n";echo "\n\n";
       }
    }

    function makeGetSetMethodsSmall(){
       foreach(get_object_vars($this) as $k => $v){
           if($k != 'dbtable' && $k != 'pkeyfld')
           echo "\nfunction set".ucfirst($k)."(\$var){".'$this->'.$k.' = $var;'."}";echo "function get".ucfirst($k)."(){return ".'$this->'.$k.";}";
       }
    }


     /**
      * Set hash-array of updated property name & new updated property value.
      *
      * @param unknown_type $arr
      */
     function setUpdatedVars($arr = array()){
         $this_arr = $this->updated_vars;
         if(!is_array($this->updated_vars)){
             $this_arr = $arr;
         }
         else{
             foreach ($arr as $key => $val){
                $this_arr[$key] = $val;
             }
         }
         $this->updated_vars = $this_arr;
     }

    function checkout($id = false){
       if($id == false or !is_numeric($id)){return false;}
       $this->setId($id);
       $db = $this->getDb();
       $data = $db->getRow("select * from ".$this->dbtable." where ".$this->pkeyfld." = '".$this->getId()."'");
	   if(is_array($data))
       foreach ($data as $kk => $vv){
           if(in_array(strtolower($kk),array_keys(get_object_vars($this)))){
               foreach(get_object_vars($this) as $k => $v){
                   $set_m = 'set'.ucfirst($k);
                   #$get_m = 'get'.ucfirst($k);
                   if(strtolower($kk) == strtolower($k)){
                       $this->$set_m($vv);
                   }
               }
           }
       }

       return $data;
    }


    function setNewValues($hash = false){
        if($hash == false or !is_array($hash)){return false;}
        foreach ($hash as $post_k => $post_v){
            if(in_array(strtolower($post_k),array_keys(get_object_vars($this)))){
               foreach(get_object_vars($this) as $this_k => $this_v){
                   if(strtolower($this_k) == $post_k){
                       $set_m = 'set'.ucfirst($this_k);
                       $get_m = 'get'.ucfirst($this_k);

                       $now_obj_value = trim($this->$get_m());

                       if($now_obj_value != $post_v){
                           #Log updated values. >>>
                           $update[$post_k] = $post_v;
                           #<<<
                           #Update class values. >>>
                           $this->$set_m($post_v);
                           #<<<
                        }
                    }
                }
            }
        }

        $this->setUpdatedVars($update);

        return $update;
    }

     function makeUpdateSqlString($hash = array()){

        $set = '';
        if(!is_array($hash)){return '';}

        foreach ($hash as $k => $v) {
                $set .= ($set == "" ? "" : ", ")."`{$k}` = '".addslashes(trim(($v)))."'";
        }

        return $set;

     }

    function setId($var){

       $this->id = $var;

    }
    function getId(){

       return $this->id;

    }

    function striptoseotitle($title = false,$non_seotitle = false){

         if(isset($title) && $title != false){
            $title = ereg_replace("[^[:alnum:]]",'-',$title);
            for($zzzz=0;$zzzz<10;$zzzz++){$title = str_replace('--','-',$title);}
         }
         else{
            $title = ereg_replace("[^[:alnum:]]",'-',$non_seotitle);
            for($zzzz=0;$zzzz<10;$zzzz++){$title = str_replace('--','-',$title);}
         }

         #check for ending '-' and strip it
         if(substr($title,-1) == '-'){
             $title = substr($title,0,-1);
         }


         return $title;
    }

    function getDateTimeforMysql($stamp = false,$quote = false){
        if($stamp == false){
            $str = date('Y-m-d H:i:s');
        }
        else{
            $str = date('Y-m-d H:i:s',$stamp);
        }

        if($quote == true){
            $str = "'".$str."'";
        }
        return $str;
    }

    /**
     * Commit changes to DB and write changes to history_log table.
     *
     */
    function commit(){

        $updateitemsql = false;
        $allset = $this->makeUpdateSqlString($this->getUpdatedVars());
        $db = $this->db;
        if($allset != '' && strlen($allset) > 2){
            $updateitemsql = 'update '.$this->dbtable.' set '.$allset.' where '.$this->pkeyfld."  =  '".$this->getId()."'";
            #echo "<br /> $updateitemsql";

            $db->Exec($updateitemsql);
        }

        $this->writeHistoryLog();
        return true;

    }

    function writeHistoryLog(){
        #dummy interface
        return true;
    }

     function getUpdatedVars(){
         return $this->updated_vars;
     }

    function setDb($var){

       $this->db = $var;

    }
    function getDb(){

       return $this->db;

    }

    /**
     * Create new record with given properties & values
     *
     */
    function create($hash = array()){
        if(count($hash) > 0){
            $db = $this->getDb();
            $db->exec('insert into '.$this->dbtable." (`".$this->pkeyfld."`) value ('')");
            $id = $db->getInsertID();

            $this->checkout($id);
            $this->setNewValues($hash);
            $this->commit();
            return $id;
        }
        else{
            return false;
        }
    }

    function delete($id = false){
        if($id == false or !is_numeric($id)){return false;}
        $db = $this->getDb();
        $db->exec('delete from '.$this->dbtable." where ".$this->pkeyfld." = '".$id."'");
        return true;

    }

    function finish(){
        return true; #dummy interface
    }

}


?>
Return current item: Abstract SQL Table to Object