<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
/**
* ThinkPHP Model模型类
* 实现了ORM和ActiveRecords模式
* @category Think
* @package Think
* @subpackage Core
* @author liu21st <liu21st@gmail.com>
*/
class Model {
// 操作状态
const MODEL_INSERT = 1; // 插入模型数据
const MODEL_UPDATE = 2; // 更新模型数据
const MODEL_BOTH = 3; // 包含上面两种方式
const MUST_VALIDATE = 1;// 必须验证
const EXISTS_VALIDATE = 0;// 表单存在字段则验证
const VALUE_VALIDATE = 2;// 表单值不为空则验证
// 当前使用的扩展模型
private $_extModel = null;
// 当前数据库操作对象
protected $db = null;
// 主键名称
protected $pk = 'id';
// 数据表前缀
protected $tablePrefix = '';
// 模型名称
protected $name = '';
// 数据库名称
protected $dbName = '';
//数据库配置
protected $connection = '';
// 数据表名(不包含表前缀)
protected $tableName = '';
// 实际数据表名(包含表前缀)
protected $trueTableName = '';
// 最近错误信息
protected $error = '';
// 字段信息
protected $fields = array();
// 数据信息
protected $data = array();
// 查询表达式参数
protected $options = array();
protected $_validate = array(); // 自动验证定义
protected $_auto = array(); // 自动完成定义
protected $_map = array(); // 字段映射定义
protected $_scope = array(); // 命名范围定义
// 是否自动检测数据表字段信息
protected $autoCheckFields = true;
// 是否批处理验证
protected $patchValidate = false;
// 链操作方法列表
protected $methods = array('table','order','alias','having','group','lock','distinct','auto','filter','validate');
/**
* 架构函数
* 取得DB类的实例对象 字段检查
* @access public
* @param string $name 模型名称
* @param string $tablePrefix 表前缀
* @param mixed $connection 数据库连接信息
*/
public function __construct($name='',$tablePrefix='',$connection='') {
// 模型初始化
$this->_initialize();
// 获取模型名称
if(!empty($name)) {
if(strpos($name,'.')) { // 支持 数据库名.模型名的 定义
list($this->dbName,$this->name) = explode('.',$name);
}else{
$this->name = $name;
}
}elseif(empty($this->name)){
$this->name = $this->getModelName();
}
// 设置表前缀
if(is_null($tablePrefix)) {// 前缀为Null表示没有前缀
$this->tablePrefix = '';
}elseif('' != $tablePrefix) {
$this->tablePrefix = $tablePrefix;
}else{
$this->tablePrefix = $this->tablePrefix?$this->tablePrefix:C('DB_PREFIX');
}
// 数据库初始化操作
// 获取数据库操作对象
// 当前模型有独立的数据库连接信息
$this->db(0,empty($this->connection)?$connection:$this->connection);
}
/**
* 自动检测数据表信息
* @access protected
* @return void
*/
protected function _checkTableInfo() {
// 如果不是Model类 自动记录数据表信息
// 只在第一次执行记录
if(empty($this->fields)) {
// 如果数据表字段没有定义则自动获取
if(C('DB_FIELDS_CACHE')) {
$db = $this->dbName?$this->dbName:C('DB_NAME');
$fields = F('_fields/'.strtolower($db.'.'.$this->name));
if($fields) {
$version = C('DB_FIELD_VERISON');
if(empty($version) || $fields['_version']== $version) {
$this->fields = $fields;
return ;
}
}
}
// 每次都会读取数据表信息
$this->flush();
}
}
/**
* 获取字段信息并缓存
* @access public
* @return void
*/
public function flush() {
// 缓存不存在则查询数据表信息
$this->db->setModel($this->name);
$fields = $this->db->getFields($this->getTableName());
if(!$fields) { // 无法获取字段信息
return false;
}
$this->fields = array_keys($fields);
$this->fields['_autoinc'] = false;
foreach ($fields as $key=>$val){
// 记录字段类型
$type[$key] = $val['type'];
if($val['primary']) {
$this->fields['_pk'] = $key;
if($val['autoinc']) $this->fields['_autoinc'] = true;
}
}
// 记录字段类型信息
$this->fields['_type'] = $type;
if(C('DB_FIELD_VERISON')) $this->fields['_version'] = C('DB_FIELD_VERISON');
// 2008-3-7 增加缓存开关控制
if(C('DB_FIELDS_CACHE')){
// 永久缓存数据表信息
$db = $this->dbName?$this->dbName:C('DB_NAME');
F('_fields/'.strtolower($db.'.'.$this->name),$this->fields);
}
}
/**
* 动态切换扩展模型
* @access public
* @param string $type 模型类型名称
* @param mixed $vars 要传入扩展模型的属性变量
* @return Model
*/
public function switchModel($type,$vars=array()) {
$class = ucwords(strtolower($type)).'Model';
if(!class_exists($class))
throw_exception($class.L('_MODEL_NOT_EXIST_'));
// 实例化扩展模型
$this->_extModel = new $class($this->name);
if(!empty($vars)) {
// 传入当前模型的属性到扩展模型
foreach ($vars as $var)
$this->_extModel->setProperty($var,$this->$var);
}
return $this->_extModel;
}
/**
* 设置数据对象的值
* @access public
* @param string $name 名称
* @param mixed $value 值
* @return void
*/
public function __set($name,$value) {
// 设置数据对象属性
$this->data[$name] = $value;
}
/**
* 获取数据对象的值
* @access public
* @param string $name 名称
* @return mixed
*/
public function __get($name) {
return isset($this->data[$name])?$this->data[$name]:null;
}
/**
* 检测数据对象的值
* @access public
* @param string $name 名称
* @return boolean
*/
public function __isset($name) {
return isset($this->data[$name]);
}
/**
* 销毁数据对象的值
* @access public
* @param string $name 名称
* @return void
*/
public function __unset($name) {
unset($this->data[$name]);
}
/
没有合适的资源?快使用搜索试试~ 我知道了~
微信公众第三方平台,乐享,pigcms源码
共1741个文件
png:439个
jpg:348个
gif:342个
4星 · 超过85%的资源 需积分: 43 100 下载量 181 浏览量
2014-01-09
16:47:13
上传
评论 7
收藏 35.93MB RAR 举报
温馨提示
整套源码 ,有比较完整的功能选项,集成有自动回复,天气,火车,翻译,陪聊基本功能,同时也有大转盘 ,刮刮卡,团购等商用功能
资源推荐
资源详情
资源评论
收起资源包目录
微信公众第三方平台,乐享,pigcms源码 (1741个子文件)
2000 508B
2000 508B
2000 508B
2000 508B
2000 508B
2000 508B
2000 508B
2000 508B
news2.css 73KB
news2.css 70KB
news5.css 69KB
news2.css 69KB
news.css 68KB
news.css 68KB
news.css 68KB
news5.css 66KB
style_2_common.css 58KB
index.css 58KB
wei_webapp_new_v1.0.4.css 40KB
style.css 39KB
style-price.css 38KB
fans.css 25KB
default.css 21KB
all.css 10KB
activity-style.css 8KB
default.css 8KB
photo.css 7KB
style1.css 7KB
style.css 7KB
main.css 6KB
basic.css 6KB
calendar.css 6KB
style.css 5KB
plugmenu.css 5KB
plugmenu.css 5KB
plugmenu.css 5KB
index_new.css 5KB
admin_style.css 5KB
style.css 4KB
datepicker.css 4KB
photoswipe.css 4KB
index.css 3KB
qq.css 3KB
style.css 3KB
qqserver.css 3KB
page.css 3KB
cate8_2.css 3KB
news2.css 3KB
news2.css 3KB
login.css 2KB
simple.css 2KB
reset.css 2KB
iscroll.css 2KB
iscroll.css 2KB
tip-yellowsimple.css 2KB
jquery.treeTable.css 2KB
tipswindown.css 1KB
prettify.css 973B
common.css 474B
WdatePicker.css 158B
style01.css 0B
news.css 0B
ie6.css 0B
Thumbs.db 75KB
Thumbs.db 74KB
Thumbs.db 54KB
Thumbs.db 49KB
Thumbs.db 18KB
Thumbs.db 14KB
Thumbs.db 14KB
Thumbs.db 10KB
Thumbs.db 9KB
Thumbs.db 9KB
Thumbs.db 8KB
Thumbs.db 8KB
Thumbs.db 7KB
Thumbs.db 7KB
Thumbs.db 6KB
Thumbs.db 6KB
Thumbs.db 6KB
Thumbs.db 6KB
Thumbs.db 5KB
Thumbs.db 5KB
Thumbs.db 3KB
bgimg_404.gif 40KB
static.gif 35KB
BIZ1.GIF 14KB
editor.gif 13KB
35.gif 13KB
35.gif 13KB
42.gif 13KB
42.gif 13KB
40.gif 10KB
40.gif 10KB
18.gif 8KB
18.gif 8KB
19.gif 8KB
19.gif 8KB
11.gif 8KB
11.gif 8KB
共 1741 条
- 1
- 2
- 3
- 4
- 5
- 6
- 18
redfox
- 粉丝: 1
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
前往页