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;
- //---------------------------- 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');
- }
- //------------------------------ check errors
- private function checkResult($result, $sql='') {
- if ($result===false)
- throw new Exception ('db error <br/>sql: '.$sql, 30044);
- }
- //------------------------------ get full table name
- public function getTableName($tabname) {
- return $this->_db->prefix.'dkrss_'.$tabname;
- }
- //------------------------------ get single record from model table
- public function getRecordById($model, $id) {
- $sql = 'select * from '.$model->_table.
- ' where '.$model->_idfield.'=%d';
- $result = $this->_db->get_row($this->_db->prepare($sql,$id), ARRAY_A);
- $this->checkResult($result, $sql);
- return array_map('stripslashes',$result);
- }
- //------------------------------ get N records from model table
- public function getNRecords($model, $start, $num) {
- $sql = 'select * from '.$model->_table.
- ($model->_filterstr ? ' where ':'').$model->_filterstr.
- ' order by '.$model->_orderstr.' LIMIT '.$start.','.$num;
- $result = $this->_db->get_results($sql, ARRAY_A);
- $this->checkResult($result, $sql);
- foreach ($result as $key=>$value) {
- $result[$key] = array_map('stripslashes', $result[$key]);
- }
- return $result;
- }
- //------------------------------ get number of records in model table
- public function getNumRecs($model) {
- $sql = 'select count(*) from '.$model->_table;
- $result = $this->_db->get_var($sql);
- $this->checkResult($result, $sql);
- return $result;
- }
- //------------------------------ inserting and updating records
- private function processRecord($model, $type='update',$id,$values) {
- $pairs = array();
- $types = array();
- $func = create_function ('$a', 'return $a["fn"];');
- $sarr = array_map($func, $model->fields);
- foreach ($values as $key=>$value) {
- $pairs[$key] = $value;
- $inf = $model->fields[array_search($key, $sarr)]['ft'];
- $types[] = '%'.($inf=='i' ? 'd' : $inf);
- }
- if ($type=='insert') {
- $rez = $this->_db->insert( $model->_table, $pairs, $types);
- $this->checkResult($rez, '');
- }
- else {
- $rez = $this->_db->update(
- $model->_table,
- $pairs, array($model->_idfield=>$id),
- $types, array('%d'));
- $this->checkResult($rez, '');
- }
- $rid = ($type=='insert') ? $this->_db->insert_id : $rez ;
- return($rid);
- }
- //-------------------------------------- insert record
- public function insertRecord($model, $values) {
- $rid = $this->processRecord ($model, 'insert', 0, $values);
- return array('message'=>'record successfully inserted', 'rid'=>$rid);
- }
- //-------------------------------------- update record
- public function updateRecord($model, $id, $values) {
- $affected_rows = $this->processRecord($model, 'update', $id, $values);
- return 'record successfully updated';
- }
- //--------------------------------------- delete record
- public function deleteRecord($model, $id) {
- $sql = 'delete from '.$model->_table.
- ' where '.$model->_idfield.'=%d';
- $rez = $this->_db->query($this->_db->prepare($sql, $id));
- $this->checkResult($rez, $sql);
- return ('record successfully deleted. affected rows: '.$rez);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement