首页 > CMS建站 > PhpCMS > 正文

【phpcms-v9】model.class.php文件分析-数据模型的基类

2020-10-10 21:03:06
字体:
来源:转载
供稿:网友
  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.     }  //Cuoxin.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. }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表