/**
* model.class.php 数据模型基类
*
* @copyright(C) 2005-2010 PHPCMS
* @licensehttp://www.phpcms.cn/license/
* @lastmodify2010-6-7
*/
//路径:phpcms/libs/classes/model.class.php数据模型基类,所有的phpcms/model/文件夹下的所有model类都继承于它
pc_base::load_sys_class('db_factory','',0);//数据库工厂类,路径:phpcms/libs/classes/db_factory.class.php
class model {
//数据库配置
protected $db_config = '';
//数据库连接
protected $db = '';
//调用数据库的配置项
protected $db_setting = 'default';
//数据表名
protected $table_name = '';
//表前缀
public $db_tablepre = '';
public function __construct() {
if (!isset($this->db_config[$this->db_setting])) {
$this->db_setting = 'default';
}
/**
* $this->db_config['default']:相当于caches/configs/database.php文件返回的数组
* return array (
'default' => array (
'hostname' => 'localhost',//主机名
'database' => 'phpcms',//数据库名
'username' => 'root',//数据库用户名
'password' => '123',//数据库密码
'tablepre' => 'v9_',//数据表前缀
'charset' => 'utf8',//数据库字符编码
'type' => 'mysql',//数据库类型,如:mysql、mysqli、access,根据不同的类型加载不同的数据库驱动
'debug' => true,
'pconnect' => 0,
'autoconnect' => 0
),
);
*/
$this->table_name = $this->db_config[$this->db_setting]['tablepre'].$this->table_name;
$this->db_tablepre = $this->db_config[$this->db_setting]['tablepre'];
/**
* 1.db_factory工厂类主要采用单利模式返回一个唯一的数据库连接实例对象
* 2.db_factory工厂类在实例化数据库实例时,会根据当前数据库的type,加载不同的数据库驱动,返回不同的数据库实例对象
* 3.db_factory工厂类通过get_instance方法从caches/configs/database.php文件中获取数据库配置信息
*/
$this->db = db_factory::get_instance($this->db_config)->get_database($this->db_setting);
}
/**
* 执行sql查询
* @param $where 查询条件[例`name`='$name']
* @param $data 需要查询的字段值[例`name`,`gender`,`birthday`]
* @param $limit 返回结果范围[例:10或10,10 默认为空]
* @param $order 排序方式[默认按数据库默认方式排序]
* @param $group 分组方式[默认为空]
* @param $key 返回数组按键名排序
* @return array查询结果集数组
*/
final public function select($where = '',$data = '*',$limit = '',$order = '',$group = '',$key='') {
if (is_array($where)) $where = $this->sqls($where);
//如果加载的数据库驱动类型是mysql,则会调用phpcms/libs/classes/mysql.class.php数据库驱动文件类库的select方法
return $this->db->select($data,$this->table_name,$where,$limit,$order,$group,$key);
}
/**
* 查询多条数据并分页
* @param $where
* @param $order
* @param $page
* @param $pagesize
* @return unknown_type
*/
final public function listinfo($where = '',$page = 1,$pagesize = 20,$key='',$setpages = 10,$urlrule = '',$array = array()) {
$where = to_sqls($where);
$this->number = $this->count($where);
$page = max(intval($page),1);
$offset = $pagesize*($page-1);
$this->pages = pages($this->number,$page,$pagesize,$urlrule,$array,$setpages);
$array = array();
if ($this->number > 0) {
return $this->select($where,'*',"$offset,$pagesize",$key);
} else {
return array();
}
}
/**
* 获取单条记录查询
* @param $where 查询条件
* @param $data 需要查询的字段值[例`name`,`birthday`]
* @param $order 排序方式[默认按数据库默认方式排序]
* @param $group 分组方式[默认为空]
* @return array/null数据查询结果集,如果不存在,则返回空
*/
final public function get_one($where = '',$group = '') {
if (is_array($where)) $where = $this->sqls($where);
//如果加载的数据库驱动类型是mysql,则会调用phpcms/libs/classes/mysql.class.php数据库驱动文件类库的get_one方法
return $this->db->get_one($data,$group);
}
/**
* 直接执行sql查询
* @param $sql查询sql语句
* @returnboolean/query resource如果为查询语句,返回资源句柄,否则返回true/false
*/
final public function query($sql) {
$sql = str_replace('phpcms_',$this->db_tablepre,$sql);
//如果加载的数据库驱动类型是mysql,则会调用phpcms/libs/classes/mysql.class.php数据库驱动文件类库的query方法
return $this->db->query($sql);
}
/**
* 执行添加记录操作
* @param $data 要增加的数据,参数为数组。数组key为字段值,数组值为数据取值
* @param $return_insert_id 是否返回新建ID号
* @param $replace 是否采用 replace into的方式添加数据
* @return boolean
*/
final public function insert($data,$return_insert_id = false,$replace = false) {
//如果加载的数据库驱动类型是mysql,则会调用phpcms/libs/classes/mysql.class.php数据库驱动文件类库的insert方法
return $this->db->insert($data,$return_insert_id,$replace);
}
/**
* 获取最后一次添加记录的主键号
* @return int
*/
final public function insert_id() {
//如果加载的数据库驱动类型是mysql,则会调用phpcms/libs/classes/mysql.class.php数据库驱动文件类库的insert_id方法
return $this->db->insert_id();
}
/**
* 执行更新记录操作
* @param $data 要更新的数据内容,参数可以为数组也可以为字符串,建议数组。
* 为数组时数组key为字段值,数组值为数据取值
* 为字符串时[例:`name`='phpcms',`hits`=`hits`+1]。
*为数组时[例: array('name'=>'phpcms','password'=>'123456')]
*数组的另一种使用array('name'=>'+=1','base'=>'-=1');程序会自动解析为`name` = `name` + 1,`base` = `base` - 1
* @param $where 更新数据时的条件,可为数组或字符串
* @return boolean
*/
final public function update($data,$where = '') {
if (is_array($where)) $where = $this->sqls($where);
//如果加载的数据库驱动类型是mysql,则会调用phpcms/libs/classes/mysql.class.php数据库驱动文件类库的update方法
return $this->db->update($data,$where);
}
/**
* 执行删除记录操作
* @param $where 删除数据条件,不充许为空。
* @return boolean
*/
final public function delete($where) {
if (is_array($where)) $where = $this->sqls($where);
//如果加载的数据库驱动类型是mysql,则会调用phpcms/libs/classes/mysql.class.php数据库驱动文件类库的delete方法
return $this->db->delete($this->table_name,$where);
}
/**
* 计算记录数
* @param string/array $where 查询条件
*/
final public function count($where = '') {
$r = $this->get_one($where,"COUNT(*) AS num");
return $r['num'];
}
/**
* 将数组转换为SQL语句
* @param array $where 要生成的数组
* @param string $font 连接串。
*/
final public function sqls($where,$font = ' AND ') {
if (is_array($where)) {
$sql = '';
foreach ($where as $key=>$val) {
$sql .= $sql ? " $font `$key` = '$val' " : " `$key` = '$val'";
}
return $sql;
} else {
return $where;
}
}
/**
* 获取最后数据库操作影响到的条数
* @return int
*/
final public function affected_rows() {
//如果加载的数据库驱动类型是mysql,则会调用phpcms/libs/classes/mysql.class.php数据库驱动文件类库的affected_rows方法
return $this->db->affected_rows();
}
/**
* 获取数据表主键
* @return array
*/
final public function get_primary() {
//如果加载的数据库驱动类型是mysql,则会调用phpcms/libs/classes/mysql.class.php数据库驱动文件类库的get_primary方法
return $this->db->get_primary($this->table_name);
}
/**
* 获取表字段
* @param string $table_name 表名
* @return array
*/
final public function get_fields($table_name = '') {
if (empty($table_name)) {
$table_name = $this->table_name;
} else {
$table_name = $this->db_tablepre.$table_name;
}
//如果加载的数据库驱动类型是mysql,则会调用phpcms/libs/classes/mysql.class.php数据库驱动文件类库的get_fields方法
return $this->db->get_fields($table_name);
}
/**
* 检查表是否存在
* @param $table 表名
* @return boolean
*/
final public function table_exists($table){
return $this->db->table_exists($this->db_tablepre.$table);
}
/**
* 检查字段是否存在
* @param $field 字段名
* @return boolean
*/
public function field_exists($field) {
$fields = $this->db->get_fields($this->table_name);
return array_key_exists($field,$fields);
}
final public function list_tables() {
return $this->db->list_tables();
}
/**
* 返回数据结果集
* @param $query (mysql_query返回值)
* @return array
*/
final public function fetch_array() {
$data = array();
while($r = $this->db->fetch_next()) {
$data[] = $r;
}
return $data;
}
/**
* 返回数据库版本号
*/
final public function version() {
return $this->db->version();
}
}