<?php
// +----------------------------------------------------------------------
// | ThinkPHP
// +----------------------------------------------------------------------
// | Copyright (c) 2008 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id$
define('HAS_ONE',1);
define('BELONGS_TO',2);
define('HAS_MANY',3);
define('MANY_TO_MANY',4);
define('MUST_TO_VALIDATE',1); // 必须验证
define('EXISTS_TO_VAILIDATE',0); // 表单存在字段则验证
define('VALUE_TO_VAILIDATE',2); // 表单值不为空则验证
/**
+------------------------------------------------------------------------------
* ThinkPHP Model模型类 抽象类
* 实现了ORM和ActiveRecords模式
+------------------------------------------------------------------------------
* @category Think
* @package Think
* @subpackage Core
* @author liu21st <liu21st@gmail.com>
* @version $Id$
+------------------------------------------------------------------------------
*/
class Model extends Base implements IteratorAggregate
{
// 数据库连接对象列表
protected $_db = array();
// 当前数据库操作对象
protected $db = null;
// 数据表前缀
protected $tablePrefix = '';
// 数据表后缀
protected $tableSuffix = '';
// 模型名称
protected $name = '';
// 数据库名称
protected $dbName = '';
// 数据表名(不包含表前缀)
protected $tableName = '';
// 实际数据表名(包含表前缀)
protected $trueTableName ='';
// 字段信息
protected $fields = array();
// 字段类型信息
protected $type = array();
// 数据信息
protected $data = array();
// 查询表达式参数
protected $options = array();
// 数据列表信息
protected $dataList = array();
// 上次错误信息
protected $error = '';
// 验证错误信息
protected $validateError = array();
// 包含的聚合对象
protected $aggregation = array();
// 是否为复合对象
protected $composite = false;
// 是否为视图模型
protected $viewModel = false;
// 乐观锁
protected $optimLock = 'lock_version';
// 悲观锁
protected $pessimisticLock = false;
protected $autoSaveRelations = false; // 自动关联保存
protected $autoDelRelations = false; // 自动关联删除
protected $autoAddRelations = false; // 自动关联写入
protected $autoReadRelations = false; // 自动关联查询
protected $lazyQuery = false; // 是否启用惰性查询
// 自动写入时间戳
protected $autoCreateTimestamps = array('create_at','create_on','cTime');
protected $autoUpdateTimestamps = array('update_at','update_on','mTime');
protected $autoTimeFormat = '';
protected $blobFields = null;
protected $blobValues = null;
/**
+----------------------------------------------------------
* 架构函数
* 取得DB类的实例对象 数据表字段检查
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param mixed $data 要创建的数据对象内容
+----------------------------------------------------------
*/
public function __construct($data='')
{
// 模型初始化
$this->_initialize();
// 模型名称获取
$this->name = $this->getModelName();
// 如果不是复合对象进行数据库初始化操作
if(!$this->composite) {
import("Think.Db.Db");
// 获取数据库操作对象
if(!empty($this->connection)) {
// 当前模型有独立的数据库连接信息
$this->db = Db::getInstance($this->connection);
}else{
$this->db = Db::getInstance();
}
// 设置数据库的返回数据格式
$this->db->resultType = C('DATA_RESULT_TYPE');
//为获得ORACLE自增LastID而统一考虑的
$this->db->tableName = $this->parseName($this->name);
// 设置默认的数据库连接
$this->_db[0] = &$this->db;
// 设置表前后缀
$this->tablePrefix = $this->tablePrefix?$this->tablePrefix:C('DB_PREFIX');
$this->tableSuffix = $this->tableSuffix?$this->tableSuffix:C('DB_SUFFIX');
// 数据表字段检测
$this->_checkTableInfo();
}
// 如果有data数据进行实例化,则创建数据对象
if(!empty($data)) {
$this->create($data);
}
}
/**
+----------------------------------------------------------
* 取得模型实例对象
+----------------------------------------------------------
* @static
* @access public
+----------------------------------------------------------
* @return mixed 返回数据模型实例
+----------------------------------------------------------
*/
public static function getInstance()
{
return get_instance_of(__CLASS__);
}
/**
+----------------------------------------------------------
* 设置数据对象的值 (魔术方法)
+----------------------------------------------------------
* @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) {
if(isset($this->data[$name])) {
return $this->data[$name];
}elseif(property_exists($this,$name)){
return $this->$name;
}else{
return null;
}
}
/**
+----------------------------------------------------------
* 字符串命名风格转换
* type
* =0 将Java风格转换为C的风格
* =1 将C风格转换为Java的风格
+----------------------------------------------------------
* @access protected
+----------------------------------------------------------
* @param string $name 字符串
* @param integer $type 转换类型
+----------------------------------------------------------
* @return string
+----------------------------------------------------------
*/
protected function parseName($name,$type=0) {
if($type) {
return preg_replace("/_([a-zA-Z])/e", "strtoupper('\\1')", $name);
}else{
$name = preg_replace("/[A-Z]/", "_\\0", $name);