ѡ̳

 找回密码
 ע
搜索
查看: 209|回复: 0
打印 上一主题 下一主题

phpcms的 基础model

[复制链接]

789

主题

1158

帖子

4197

积分

Ա

Rank: 9Rank: 9Rank: 9

积分
4197
跳转到指定楼层
¥
发表于 2017-11-7 04:23:26 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

源码
  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. defined('IN_PHPCMS') or exit('Access Denied');
  10. pc_base::load_sys_class('db_factory', '', 0);
  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.                 $this->table_name = $this->db_config[$this->db_setting]['tablepre'].$this->table_name;
  29.                 $this->db_tablepre = $this->db_config[$this->db_setting]['tablepre'];
  30.                 $this->db = db_factory::get_instance($this->db_config)->get_database($this->db_setting);
  31.         }
  32.                
  33.         /**
  34.          * 执行sql查询
  35.          * @param $where                 查询条件[例`name`='$name']
  36.          * @param $data                 需要查询的字段值[例`name`,`gender`,`birthday`]
  37.          * @param $limit                 返回结果范围[例:10或10,10 默认为空]
  38.          * @param $order                 排序方式        [默认按数据库默认方式排序]
  39.          * @param $group                 分组方式        [默认为空]
  40.          * @param $key          返回数组按键名排序
  41.          * @return array                查询结果集数组
  42.          */
  43.         final public function select($where = '', $data = '*', $limit = '', $order = '', $group = '', $key='') {
  44.                 if (is_array($where)) $where = $this->sqls($where);
  45.                 return $this->db->select($data, $this->table_name, $where, $limit, $order, $group, $key);
  46.         }

  47.         /**
  48.          * 查询多条数据并分页
  49.          * @param $where
  50.          * @param $order
  51.          * @param $page
  52.          * @param $pagesize
  53.          * @return unknown_type
  54.          */
  55.         final public function listinfo($where = '', $order = '', $page = 1, $pagesize = 20, $key='', $setpages = 10,$urlrule = '',$array = array(), $data = '*') {
  56.                 $where = to_sqls($where);
  57.                 $this->number = $this->count($where);
  58.                 $page = max(intval($page), 1);
  59.                 $offset = $pagesize*($page-1);
  60.                 $this->pages = pages($this->number, $page, $pagesize, $urlrule, $array, $setpages);
  61.                 $array = array();
  62.                 if ($this->number > 0) {
  63.                         return $this->select($where, $data, "$offset, $pagesize", $order, '', $key);
  64.                 } else {
  65.                         return array();
  66.                 }
  67.         }

  68.         /**
  69.          * 获取单条记录查询
  70.          * @param $where                 查询条件
  71.          * @param $data                 需要查询的字段值[例`name`,`gender`,`birthday`]
  72.          * @param $order                 排序方式        [默认按数据库默认方式排序]
  73.          * @param $group                 分组方式        [默认为空]
  74.          * @return array/null        数据查询结果集,如果不存在,则返回空
  75.          */
  76.         final public function get_one($where = '', $data = '*', $order = '', $group = '') {
  77.                 if (is_array($where)) $where = $this->sqls($where);
  78.                 return $this->db->get_one($data, $this->table_name, $where, $order, $group);
  79.         }
  80.        
  81.         /**
  82.          * 直接执行sql查询
  83.          * @param $sql                                                        查询sql语句
  84.          * @return        boolean/query resource                如果为查询语句,返回资源句柄,否则返回true/false
  85.          */
  86.         final public function query($sql) {
  87.                 $sql = str_replace('phpcms_', $this->db_tablepre, $sql);
  88.                 return $this->db->query($sql);
  89.         }
  90.        
  91.         /**
  92.          * 执行添加记录操作
  93.          * @param $data                 要增加的数据,参数为数组。数组key为字段值,数组值为数据取值
  94.          * @param $return_insert_id 是否返回新建ID号
  95.          * @param $replace 是否采用 replace into的方式添加数据
  96.          * @return boolean
  97.          */
  98.         final public function insert($data, $return_insert_id = false, $replace = false) {
  99.                 return $this->db->insert($data, $this->table_name, $return_insert_id, $replace);
  100.         }
  101.        
  102.         /**
  103.          * 获取最后一次添加记录的主键号
  104.          * @return int
  105.          */
  106.         final public function insert_id() {
  107.                 return $this->db->insert_id();
  108.         }
  109.        
  110.         /**
  111.          * 执行更新记录操作
  112.          * @param $data                 要更新的数据内容,参数可以为数组也可以为字符串,建议数组。
  113.          *                                                 为数组时数组key为字段值,数组值为数据取值
  114.          *                                                 为字符串时[例:`name`='phpcms',`hits`=`hits`+1]。
  115.          *                                                为数组时[例: array('name'=>'phpcms','password'=>'123456')]
  116.          *                                                数组的另一种使用array('name'=>'+=1', 'base'=>'-=1');程序会自动解析为`name` = `name` + 1, `base` = `base` - 1
  117.          * @param $where                 更新数据时的条件,可为数组或字符串
  118.          * @return boolean
  119.          */
  120.         final public function update($data, $where = '') {
  121.                 if (is_array($where)) $where = $this->sqls($where);
  122.                 return $this->db->update($data, $this->table_name, $where);
  123.         }
  124.        
  125.         /**
  126.          * 执行删除记录操作
  127.          * @param $where                 删除数据条件,不充许为空。
  128.          * @return boolean
  129.          */
  130.         final public function delete($where) {
  131.                 if (is_array($where)) $where = $this->sqls($where);
  132.                 return $this->db->delete($this->table_name, $where);
  133.         }
  134.        
  135.         /**
  136.          * 计算记录数
  137.          * @param string/array $where 查询条件
  138.          */
  139.         final public function count($where = '') {
  140.                 $r = $this->get_one($where, "COUNT(*) AS num");
  141.                 return $r['num'];
  142.         }
  143.        
  144.         /**
  145.          * 将数组转换为SQL语句
  146.          * @param array $where 要生成的数组
  147.          * @param string $font 连接串。
  148.          */
  149.         final public function sqls($where, $font = ' AND ') {
  150.                 if (is_array($where)) {
  151.                         $sql = '';
  152.                         foreach ($where as $key=>$val) {
  153.                                 $sql .= $sql ? " $font `$key` = '$val' " : " `$key` = '$val'";
  154.                         }
  155.                         return $sql;
  156.                 } else {
  157.                         return $where;
  158.                 }
  159.         }
  160.        
  161.         /**
  162.          * 获取最后数据库操作影响到的条数
  163.          * @return int
  164.          */
  165.         final public function affected_rows() {
  166.                 return $this->db->affected_rows();
  167.         }
  168.        
  169.         /**
  170.          * 获取数据表主键
  171.          * @return array
  172.          */
  173.         final public function get_primary() {
  174.                 return $this->db->get_primary($this->table_name);
  175.         }
  176.        
  177.         /**
  178.          * 获取表字段
  179.          * @param string $table_name    表名
  180.          * @return array
  181.          */
  182.         final public function get_fields($table_name = '') {
  183.                 if (empty($table_name)) {
  184.                         $table_name = $this->table_name;
  185.                 } else {
  186.                         $table_name = $this->db_tablepre.$table_name;
  187.                 }
  188.                 return $this->db->get_fields($table_name);
  189.         }
  190.        
  191.         /**
  192.          * 检查表是否存在
  193.          * @param $table 表名
  194.          * @return boolean
  195.          */
  196.         final public function table_exists($table){
  197.                 return $this->db->table_exists($this->db_tablepre.$table);
  198.         }
  199.        
  200.         /**
  201.          * 检查字段是否存在
  202.          * @param $field 字段名
  203.          * @return boolean
  204.          */
  205.         public function field_exists($field) {
  206.                 $fields = $this->db->get_fields($this->table_name);
  207.                 return array_key_exists($field, $fields);
  208.         }
  209.        
  210.         final public function list_tables() {
  211.                 return $this->db->list_tables();
  212.         }
  213.         /**
  214.          * 返回数据结果集
  215.          * @param $query (mysql_query返回值)
  216.          * @return array
  217.          */
  218.         final public function fetch_array() {
  219.                 $data = array();
  220.                 while($r = $this->db->fetch_next()) {
  221.                         $data[] = $r;               
  222.                 }
  223.                 return $data;
  224.         }
  225.        
  226.         /**
  227.          * 返回数据库版本号
  228.          */
  229.         final public function version() {
  230.                 return $this->db->version();
  231.         }
  232. }
复制代码




回复

使用道具 举报

您需要登录后才可以回帖 登录 | ע

本版积分规则

QQ|Archiver|ֻ|С|ѡ̳

GMT+8, 2026-5-2 07:26 , Processed in 0.078325 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表
0.0918s