国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > CMS > PhpCMS > 正文

【phpcms-v9】model.class.php文件分析-數據模型的基類

2024-09-10 07:15:36
字體:
來源:轉載
供稿:網友
  1. <?php    
  2. /**   
  3.  *  model.class.php 數據模型基類   
  4.  *   
  5.  * @copyright           (C) 2005-2010 PHPCMS   
  6.  * @license             http://www.phpcms.cn/license/   
  7.  * @lastmodify          2010-6-7   
  8.  */   
  9. //路徑:phpcms/libs/classes/model.class.php數據模型基類,所有的phpcms/model/文件夾下的所有model類都繼承于它   
  10. pc_base::load_sys_class('db_factory''', 0);//數據庫工廠類,路徑:phpcms/libs/classes/db_factory.class.php   
  11. class model {   
  12.        
  13.     //數據庫配置   
  14.     protected $db_config = '';   
  15.     //數據庫連接   
  16.     protected $db = '';   
  17.     //調用數據庫的配置項   
  18.     protected $db_setting = 'default';   
  19.     //數據表名   
  20.     protected $table_name = '';   
  21.     //表前綴   
  22.     public  $db_tablepre = '';   
  23.        
  24.     public function __construct() {   
  25.         if (!isset($this->db_config[$this->db_setting])) {   
  26.             $this->db_setting = 'default';   
  27.         }   
  28.         /**   
  29.          * $this->db_config['default']:相當于caches/configs/database.php文件返回的數組   
  30.          * return array (   
  31.                 'default' => array (   
  32.                     'hostname' => 'localhost',           //主機名   
  33.                     'database' => 'phpcms',              //數據庫名   
  34.                     'username' => 'root',                //數據庫用戶名   
  35.                     'password' => '123',             //數據庫密碼   
  36.                     'tablepre' => 'v9_',             //數據表前綴   
  37.                     'charset' => 'utf8',             //數據庫字符編碼   
  38.                     'type' => 'mysql',               //數據庫類型,如:mysql、mysqli、access,根據不同的類型加載不同的數據庫驅動   
  39.                     'debug' => true,   
  40.                     'pconnect' => 0,   
  41.                     'autoconnect' => 0   
  42.                     ),   
  43.             );   
  44.          */   
  45.         $this->table_name = $this->db_config[$this->db_setting]['tablepre'].$this->table_name;   
  46.         $this->db_tablepre = $this->db_config[$this->db_setting]['tablepre'];   
  47.         /**   
  48.          * 1.db_factory工廠類主要采用單利模式返回一個唯一的數據庫連接實例對象   
  49.          * 2.db_factory工廠類在實例化數據庫實例時,會根據當前數據庫的type,加載不同的數據庫驅動,返回不同的數據庫實例對象   
  50.                  * 3.db_factory工廠類通過get_instance方法從caches/configs/database.php文件中獲取數據庫配置信息   
  51.                  */   
  52.         $this->db = db_factory::get_instance($this->db_config)->get_database($this->db_setting);   
  53.     }   
  54.            
  55.     /**   
  56.      * 執行sql查詢   
  57.      * @param $where        查詢條件[例`name`='$name']   
  58.      * @param $data         需要查詢的字段值[例`name`,`gender`,`birthday`]   
  59.      * @param $limit        返回結果范圍[例:10或10,10 默認為空]   
  60.      * @param $order        排序方式    [默認按數據庫默認方式排序]   
  61.      * @param $group        分組方式    [默認為空]   
  62.      * @param $key          返回數組按鍵名排序   
  63.      * @return array        查詢結果集數組   
  64.      */   
  65.     final public function select($where = ''$data = '*'$limit = ''$order = ''$group = ''$key='') {   
  66.         if (is_array($where)) $where = $this->sqls($where);   
  67.         //如果加載的數據庫驅動類型是mysql,則會調用phpcms/libs/classes/mysql.class.php數據庫驅動文件類庫的select方法   
  68.         return $this->db->select($data$this->table_name, $where$limit$order$group$key);   
  69.     }   
  70.    
  71.     /**   
  72.      * 查詢多條數據并分頁   
  73.      * @param $where   
  74.      * @param $order   
  75.      * @param $page   
  76.      * @param $pagesize   
  77.      * @return unknown_type   
  78.      */   
  79.     final public function listinfo($where = ''$order = ''$page = 1, $pagesize = 20, $key=''$setpages = 10,$urlrule = '',$array = array()) {   
  80.         $where = to_sqls($where);   
  81.         $this->number = $this->count($where);   
  82.         $page = max(intval($page), 1);   
  83.         $offset = $pagesize*($page-1);   
  84.         $this->pages = pages($this->number, $page$pagesize$urlrule$array$setpages);   
  85.         $array = array();   
  86.         if ($this->number > 0) {   
  87.             return $this->select($where'*'"$offset, $pagesize"$order''$key);   
  88.         } else {   
  89.             return array();   
  90.         }   
  91.     }   
  92.    
  93.     /**   
  94.      * 獲取單條記錄查詢   
  95.      * @param $where        查詢條件   
  96.      * @param $data         需要查詢的字段值[例`name`,`gender`,`birthday`]   
  97.      * @param $order        排序方式    [默認按數據庫默認方式排序]   
  98.      * @param $group        分組方式    [默認為空]   
  99.      * @return array/null   數據查詢結果集,如果不存在,則返回空   
  100.      */   
  101.     final public function get_one($where = ''$data = '*'$order = ''$group = '') {   
  102.         if (is_array($where)) $where = $this->sqls($where);   
  103.         //如果加載的數據庫驅動類型是mysql,則會調用phpcms/libs/classes/mysql.class.php數據庫驅動文件類庫的get_one方法   
  104.         return $this->db->get_one($data$this->table_name, $where$order$group);   
  105.     }   
  106.        
  107.     /**   
  108.      * 直接執行sql查詢   
  109.      * @param $sql                          查詢sql語句   
  110.      * @return  boolean/query resource      如果為查詢語句,返回資源句柄,否則返回true/false   
  111.      */   
  112.     final public function query($sql) {   
  113.         $sql = str_replace('phpcms_'$this->db_tablepre, $sql);   
  114.         //如果加載的數據庫驅動類型是mysql,則會調用phpcms/libs/classes/mysql.class.php數據庫驅動文件類庫的query方法   
  115.         return $this->db->query($sql);   
  116.     }   
  117.        
  118.     /**   
  119.      * 執行添加記錄操作   
  120.      * @param $data         要增加的數據,參數為數組。數組key為字段值,數組值為數據取值   
  121.      * @param $return_insert_id 是否返回新建ID號   
  122.      * @param $replace 是否采用 replace into的方式添加數據   
  123.      * @return boolean   
  124.      */   
  125.     final public function insert($data$return_insert_id = false, $replace = false) {   
  126.         //如果加載的數據庫驅動類型是mysql,則會調用phpcms/libs/classes/mysql.class.php數據庫驅動文件類庫的insert方法   
  127.         return $this->db->insert($data$this->table_name, $return_insert_id$replace);   
  128.     }   
  129.        
  130.     /**   
  131.      * 獲取最后一次添加記錄的主鍵號   
  132.      * @return int    
  133.      */   
  134.     final public function insert_id() {   
  135.         //如果加載的數據庫驅動類型是mysql,則會調用phpcms/libs/classes/mysql.class.php數據庫驅動文件類庫的insert_id方法   
  136.         return $this->db->insert_id();   
  137.     }   
  138.        
  139.     /**   
  140.      * 執行更新記錄操作   
  141.      * @param $data         要更新的數據內容,參數可以為數組也可以為字符串,建議數組。   
  142.      *                      為數組時數組key為字段值,數組值為數據取值   
  143.      *                      為字符串時[例:`name`='phpcms',`hits`=`hits`+1]。   
  144.      *                      為數組時[例: array('name'=>'phpcms','password'=>'123456')]   
  145.      *                      數組的另一種使用array('name'=>'+=1', 'base'=>'-=1');程序會自動解析為`name` = `name` + 1, `base` = `base` - 1   
  146.      * @param $where        更新數據時的條件,可為數組或字符串   
  147.      * @return boolean   
  148.      */   
  149.     final public function update($data$where = '') {   
  150.         if (is_array($where)) $where = $this->sqls($where);   
  151.         //如果加載的數據庫驅動類型是mysql,則會調用phpcms/libs/classes/mysql.class.php數據庫驅動文件類庫的update方法   
  152.         return $this->db->update($data$this->table_name, $where);   
  153.     }   
  154.        
  155.     /**   
  156.      * 執行刪除記錄操作   
  157.      * @param $where        刪除數據條件,不充許為空。   
  158.      * @return boolean   
  159.      */   
  160.     final public function delete($where) {   
  161.         if (is_array($where)) $where = $this->sqls($where);   
  162.         //如果加載的數據庫驅動類型是mysql,則會調用phpcms/libs/classes/mysql.class.php數據庫驅動文件類庫的delete方法   
  163.         return $this->db->delete($this->table_name, $where);   
  164.     }   
  165.        
  166.     /**   
  167.      * 計算記錄數   
  168.      * @param string/array $where 查詢條件   
  169.      */   
  170.     final public function count($where = '') {   
  171.         $r = $this->get_one($where"COUNT(*) AS num");   
  172.         return $r['num'];   
  173.     }   
  174.        
  175.     /**   
  176.      * 將數組轉換為SQL語句   
  177.      * @param array $where 要生成的數組   
  178.      * @param string $font 連接串。   
  179.      */   
  180.     final public function sqls($where$font = ' AND ') {   
  181.         if (is_array($where)) {   
  182.             $sql = '';   
  183.             foreach ($where as $key=>$val) {   
  184.                 $sql .= $sql ? " $font `$key` = '$val' " : " `$key` = '$val'";   
  185.             }   
  186.             return $sql;   
  187.         } else {   
  188.             return $where;   
  189.         }   
  190.     }   
  191.        
  192.     /**   
  193.      * 獲取最后數據庫操作影響到的條數   
  194.      * @return int   
  195.      */   
  196.     final public function affected_rows() {   
  197.         //如果加載的數據庫驅動類型是mysql,則會調用phpcms/libs/classes/mysql.class.php數據庫驅動文件類庫的affected_rows方法   
  198.         return $this->db->affected_rows();   
  199.     }   
  200.        
  201.     /**   
  202.      * 獲取數據表主鍵   
  203.      * @return array   
  204.      */   
  205.     final public function get_primary() {   
  206.         //如果加載的數據庫驅動類型是mysql,則會調用phpcms/libs/classes/mysql.class.php數據庫驅動文件類庫的get_primary方法   
  207.         return $this->db->get_primary($this->table_name);   
  208.     }   
  209.        
  210.     /**   
  211.      * 獲取表字段   
  212.      * @param string $table_name    表名   
  213.      * @return array   
  214.      */   
  215.     final public function get_fields($table_name = '') {   
  216.         if (emptyempty($table_name)) {   
  217.             $table_name = $this->table_name;   
  218.         } else {   
  219.             $table_name = $this->db_tablepre.$table_name;   
  220.         }   
  221.         //如果加載的數據庫驅動類型是mysql,則會調用phpcms/libs/classes/mysql.class.php數據庫驅動文件類庫的get_fields方法   
  222.         return $this->db->get_fields($table_name);   
  223.     }   
  224.        
  225.     /**   
  226.      * 檢查表是否存在   
  227.      * @param $table 表名   
  228.      * @return boolean   
  229.      */   
  230.     final public function table_exists($table){   
  231.         return $this->db->table_exists($this->db_tablepre.$table);   
  232.     }   
  233.        
  234.     /**   
  235.      * 檢查字段是否存在   
  236.      * @param $field 字段名   
  237.      * @return boolean   
  238.      */   
  239.     public function field_exists($field) {   
  240.         $fields = $this->db->get_fields($this->table_name);   
  241.         return array_key_exists($field$fields);   
  242.     }  //Vevb.com 
  243.        
  244.     final public function list_tables() {   
  245.         return $this->db->list_tables();   
  246.     }   
  247.     /**   
  248.      * 返回數據結果集   
  249.      * @param $query (mysql_query返回值)   
  250.      * @return array   
  251.      */   
  252.     final public function fetch_array() {   
  253.         $data = array();   
  254.         while($r = $this->db->fetch_next()) {   
  255.             $data[] = $r;          
  256.         }   
  257.         return $data;   
  258.     }   
  259.        
  260.     /**   
  261.      * 返回數據庫版本號   
  262.      */   
  263.     final public function version() {   
  264.         return $this->db->version();   
  265.     }   
  266. }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 区。| 称多县| 阳城县| 建德市| 桐梓县| 蒙阴县| 广宁县| 延边| 耿马| 遵义县| 关岭| 乌兰浩特市| 陵水| 新乐市| 肃宁县| 图片| 肇源县| 宁武县| 探索| 湄潭县| 永新县| 府谷县| 武冈市| 梁河县| 新蔡县| 收藏| 清涧县| 和顺县| 龙门县| 古丈县| 房产| 多伦县| 平阴县| 衡南县| 广饶县| 琼海市| 通许县| 集贤县| 商南县| 大丰市| 寻甸|