Advertisement
terorama

WP / test / mysqli / dbmodel.inc.php

May 29th, 2013
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.36 KB | None | 0 0
  1. <?php
  2.  
  3. //----------------------------------------------
  4. //                  DBModel
  5. //----------------------------------------------
  6. class DBModel implements IDBModel, IInjectable {
  7.  
  8.    private $_debug;
  9.    private $_db;
  10.    private $_model;
  11.    
  12.    
  13.    //---------------------------- debug information
  14.    private function _w($info, $method) {
  15.    
  16.       $className = get_class($this);
  17.       $this->_debug->writeInfo($info, $className, $method);
  18.    }
  19.    //------------------------------ constructor
  20.    
  21.    public function __construct($injector) {
  22.  
  23.       $this->_db = $injector->get('IDBConn')->get();
  24.       $this->_debug = $injector->get('IDebug');
  25.    }
  26.    
  27.    //------------------------------
  28.    public function setup($model) {
  29.       $this->_model = $model;
  30.    }
  31.    
  32.    //------------------------------ check errors
  33.    
  34.    private function checkResult($result, $err, $sql='') {
  35.    
  36.       if ($result===false)
  37.          throw new Exception ('db error '.$err.'<br/>sql: '.$sql, 30044);
  38.    }
  39.    
  40.    //------------------------------ get full table name
  41.    
  42.    public function getTableName($tabname) {
  43.       return $tabname;
  44.    }
  45.    
  46.    //------------------------------ get single record from model table
  47.    
  48.    public function getRecordById($id) {
  49.    
  50.            
  51.        $sql =  'select * from '.$this->_model->_table.
  52.        ' where '.$this->_model->_idfield.'='. $this->_db->real_escape_string($id);
  53.                                          
  54.        $result = $this->_db->query($sql);
  55.        $this->checkResult($result, $this->_db->error, $sql);
  56.        
  57.        $row = $result->fetch_array(MYSQLI_ASSOC);
  58.        
  59.        $result->close();
  60.      
  61.        return $row;
  62.     }
  63.  
  64.    //------------------------------ get N records from model table
  65.    
  66.    public function getNRecords($start, $num) {
  67.      
  68.      
  69.       $sql =  'select * from '.$this->_model->_table.
  70.                ($this->_model->_filterstr ? ' where ':'').$this->_model->_filterstr.
  71.               ' order by '.$this->_model->_orderstr;
  72.                
  73.       $result = $this->_db->query($sql);
  74.      
  75.       $this->checkResult($result, $this->_db->error, $sql);
  76.          
  77.       $result->data_seek($start);
  78.      
  79.       $i=0;
  80.       $ret = array();
  81.       while (($row = $result->fetch_assoc()) && ($i<$num)) {
  82.      
  83.          $ret[] = $row;
  84.          $i++;
  85.       }
  86.       $result->close();
  87.       //$this->_w($ret, __METHOD__);
  88.       return $ret;
  89.    }
  90.  
  91.    //------------------------------ get number of records in model table
  92.    
  93.   public function getNumRecs() {
  94.    
  95.       $sql = 'select count(*) from '.$this->_model->_table;
  96.       $result = $this->_db->query($sql);
  97.      
  98.       $this->checkResult($result, $this->_db->error, $sql);
  99.      
  100.       $inf = $result->fetch_row();
  101.       $result->close();
  102.      
  103.       return $inf[0];
  104.      
  105.    }
  106.    
  107.    //------------------------------ inserting and updating records
  108.    
  109.    private function processRecord($type='update',$id,$values) {
  110.      
  111.       $flds = array();
  112.       $types = array();
  113.       $invars = array();
  114.      
  115.       $func = create_function ('$a', 'return $a["fn"];');
  116.       $func4 = create_function('$a','return $a."=?";');
  117.      
  118.       //$this->_w($values,__METHOD__);
  119.      
  120.       $sarr = array_map($func, $this->_model->fields);
  121.      
  122.       foreach ($values as $key=>$value) {
  123.          
  124.          $flds[] = $key;
  125.          $invars[] = & $values[$key];
  126.            
  127.          $types[] = $this->_model->fields[array_search($key, $sarr)]['ft'];
  128.       }
  129.       //--------------------------
  130.      
  131.       if ($type=='insert') {
  132.      
  133.          $flds_s = implode(',',$flds);
  134.          $valpos = implode(',' , array_fill(0, count($flds),'?'));
  135.          
  136.       } else {
  137.          
  138.          $fld_s = implode(',' , array_map($func4, $flds));
  139.       }
  140.       //--------------------------
  141.       $types_s = implode('',$types);
  142.      
  143.       array_unshift($invars, $types_s);
  144.      
  145.       //---------------------------
  146.       if ($type=='insert')
  147.          $sql = 'insert into '.$this->_model->_table.'('.$flds_s.') values ('.$valpos.')';
  148.       else
  149.          $sql = 'update '.$this->_model->_table.' set '.$fld_s.
  150.           ' where '.$this->_model->_idfield.'='.$this->_db->real_escape_string($id);
  151.      
  152.       //---------------------------
  153.      
  154.       $stmt = $this->_db->prepare ($sql);
  155.      
  156.      
  157.       call_user_func_array(array($stmt,'bind_param'), $invars);
  158.      
  159.       $rez = $stmt->execute();
  160.       $this->checkResult($rez, $stmt->error, $sql);
  161.      
  162.       $rid = ($type=='insert') ? $stmt->insert_id : $stmt->affected_rows ;
  163.       $stmt->close();
  164.      
  165.       return($rid);
  166.  
  167.    }
  168.    //-------------------------------------- insert record  
  169.    public function insertRecord($values) {
  170.    
  171.       $rid = $this->processRecord ('insert', 0, $values);    
  172.       return array('message'=>'record successfully inserted. record id: '.$rid, 'rid'=>$rid);
  173.    }
  174.    
  175.    //-------------------------------------- update record
  176.    
  177.    public function updateRecord($id, $values) {
  178.    
  179.       $affected_rows = $this->processRecord('update', $id, $values);
  180.       return 'record successfully updated. affected rows: '.$affected_rows;
  181.    }
  182.    
  183.    //--------------------------------------- delete record
  184.    public function deleteRecord($id) {
  185.    
  186.       $sql = 'delete from '.$this->_model->_table.' where '.$this->_model->_idfield.'=?';
  187.       $stmt = $this->_db->prepare($sql);
  188.      
  189.       $stmt->bind_param('i', $id);
  190.      
  191.       $rez = $stmt->execute();
  192.       $this->checkResult($rez, $stmt->error, $sql);
  193.      
  194.       $affected_rows = $stmt->affected_rows;
  195.       $stmt->close();
  196.      
  197.       return ('record successfully deleted. affected rows: '.$affected_rows);
  198.    }
  199. }
  200.  
  201. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement