Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //----------------------------------------------
- // DBModel
- //----------------------------------------------
- class DBModel implements IDBModel, IInjectable {
- private $_debug;
- private $_db;
- private $_model;
- //---------------------------- debug information
- private function _w($info, $method) {
- $className = get_class($this);
- $this->_debug->writeInfo($info, $className, $method);
- }
- //------------------------------ constructor
- public function __construct($injector) {
- $this->_db = $injector->get('IDBConn')->get();
- $this->_debug = $injector->get('IDebug');
- }
- //------------------------------
- public function setup($model) {
- $this->_model = $model;
- }
- //------------------------------ check errors
- private function checkResult($result, $err, $sql='') {
- if ($result===false)
- throw new Exception ('db error '.$err.'<br/>sql: '.$sql, 30044);
- }
- //------------------------------ get full table name
- public function getTableName($tabname) {
- return $tabname;
- }
- //------------------------------ get single record from model table
- public function getRecordById($id) {
- $sql = 'select * from '.$this->_model->_table.
- ' where '.$this->_model->_idfield.'='. $this->_db->real_escape_string($id);
- $result = $this->_db->query($sql);
- $this->checkResult($result, $this->_db->error, $sql);
- $row = $result->fetch_array(MYSQLI_ASSOC);
- $result->close();
- return $row;
- }
- //------------------------------ get N records from model table
- public function getNRecords($start, $num) {
- $sql = 'select * from '.$this->_model->_table.
- ($this->_model->_filterstr ? ' where ':'').$this->_model->_filterstr.
- ' order by '.$this->_model->_orderstr;
- $result = $this->_db->query($sql);
- $this->checkResult($result, $this->_db->error, $sql);
- $result->data_seek($start);
- $i=0;
- $ret = array();
- while (($row = $result->fetch_assoc()) && ($i<$num)) {
- $ret[] = $row;
- $i++;
- }
- $result->close();
- //$this->_w($ret, __METHOD__);
- return $ret;
- }
- //------------------------------ get number of records in model table
- public function getNumRecs() {
- $sql = 'select count(*) from '.$this->_model->_table;
- $result = $this->_db->query($sql);
- $this->checkResult($result, $this->_db->error, $sql);
- $inf = $result->fetch_row();
- $result->close();
- return $inf[0];
- }
- //------------------------------ inserting and updating records
- private function processRecord($type='update',$id,$values) {
- $flds = array();
- $types = array();
- $invars = array();
- $func = create_function ('$a', 'return $a["fn"];');
- $func4 = create_function('$a','return $a."=?";');
- //$this->_w($values,__METHOD__);
- $sarr = array_map($func, $this->_model->fields);
- foreach ($values as $key=>$value) {
- $flds[] = $key;
- $invars[] = & $values[$key];
- $types[] = $this->_model->fields[array_search($key, $sarr)]['ft'];
- }
- //--------------------------
- if ($type=='insert') {
- $flds_s = implode(',',$flds);
- $valpos = implode(',' , array_fill(0, count($flds),'?'));
- } else {
- $fld_s = implode(',' , array_map($func4, $flds));
- }
- //--------------------------
- $types_s = implode('',$types);
- array_unshift($invars, $types_s);
- //---------------------------
- if ($type=='insert')
- $sql = 'insert into '.$this->_model->_table.'('.$flds_s.') values ('.$valpos.')';
- else
- $sql = 'update '.$this->_model->_table.' set '.$fld_s.
- ' where '.$this->_model->_idfield.'='.$this->_db->real_escape_string($id);
- //---------------------------
- $stmt = $this->_db->prepare ($sql);
- call_user_func_array(array($stmt,'bind_param'), $invars);
- $rez = $stmt->execute();
- $this->checkResult($rez, $stmt->error, $sql);
- $rid = ($type=='insert') ? $stmt->insert_id : $stmt->affected_rows ;
- $stmt->close();
- return($rid);
- }
- //-------------------------------------- insert record
- public function insertRecord($values) {
- $rid = $this->processRecord ('insert', 0, $values);
- return array('message'=>'record successfully inserted. record id: '.$rid, 'rid'=>$rid);
- }
- //-------------------------------------- update record
- public function updateRecord($id, $values) {
- $affected_rows = $this->processRecord('update', $id, $values);
- return 'record successfully updated. affected rows: '.$affected_rows;
- }
- //--------------------------------------- delete record
- public function deleteRecord($id) {
- $sql = 'delete from '.$this->_model->_table.' where '.$this->_model->_idfield.'=?';
- $stmt = $this->_db->prepare($sql);
- $stmt->bind_param('i', $id);
- $rez = $stmt->execute();
- $this->checkResult($rez, $stmt->error, $sql);
- $affected_rows = $stmt->affected_rows;
- $stmt->close();
- return ('record successfully deleted. affected rows: '.$affected_rows);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement