李景山php,[李景山php]每天TP5-20170108|thinkphp5-Model.php-1

// +----------------------------------------------------------------------

// | ThinkPHP [ WE CAN DO IT JUST THINK ]

// +----------------------------------------------------------------------

// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.

// +----------------------------------------------------------------------

// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )

// +----------------------------------------------------------------------

// | Author: liu21st 

// +----------------------------------------------------------------------

namespace think;

// 命名空间 跟目录 think

use InvalidArgumentException;// 系统 错误 异常

use think\Cache;// 缓存

use think\Db;// 数据库

use think\db\Query; // 查询语句

use think\Exception;// 异常

use think\Exception\ValidateException;// 验证异常

use think\Loader;// 加载类

use think\model\Relation;// 关系类

use think\paginator\Collection as PaginatorCollection;// 分页 类

/**

* Class Model

* @package think

* @method static PaginatorCollection paginate(integer $listRows = 15, boolean $simple = false, array $config = []) 分页查询

* @method static mixed value($field, $default = null) 得到某个字段的值

* @method static array column($field, $key = '') 得到某个列的数组

* @method static integer count($field = '*') COUNT查询

* @method static integer sum($field = '*') SUM查询

* @method static integer min($field = '*') MIN查询

* @method static integer max($field = '*') MAX查询

* @method static integer avg($field = '*') AVG查询

* @method static setField($field, $value = '')

* @method static Query where($field, $op = null, $condition = null) 指定AND查询条件

* @method static static findOrFail($data = null) 查找单条记录 如果不存在则抛出异常 Fail 异常

*

*/

abstract class Model implements \JsonSerializable, \ArrayAccess

{// 抽象类 继承了 JsonSerializable, ArrayAccess

// 数据库对象池

protected static $links = [];// 数据库 对象 连接池

// 数据库配置

protected $connection = [];// 数据库 配置项目

// 当前模型名称

protected $name;// 模型名称

// 数据表名称

protected $table;// 表名称

// 当前类名称

protected $class;// 类名

// 回调事件

private static $event = [];// 回调事件

// 错误信息

protected $error;// 错误信息

// 字段验证规则

protected $validate;// 验证规则

// 数据表主键 复合主键使用数组定义 不设置则自动获取

protected $pk;// 主键

// 数据表字段信息 留空则自动获取

protected $field = [];// 数据表 字段信息 留空 就自动获取

// 只读字段

protected $readonly = [];// 只读字段

// 显示属性

protected $visible = [];// 显示属性

// 隐藏属性

protected $hidden = [];// 隐藏 属性

// 追加属性

protected $append = [];// 追加属性

// 数据信息

protected $data = [];// 数据信息

// 记录改变字段

protected $change = [];// 改变字段

// 保存自动完成列表

protected $auto = [];// 保存自动完成列表

// 新增自动完成列表

protected $insert = [];// 新增 自动完成列表

// 更新自动完成列表

protected $update = [];// 更新自动完成列表

// 是否需要自动写入时间戳 如果设置为字符串 则表示时间字段的类型

protected $autoWriteTimestamp;// 是否需要自动写入时间戳,如果设置为字符串, 则表示时间字段的类型

// 创建时间字段

protected $createTime = 'create_time';// 创建时间字段

// 更新时间字段

protected $updateTime = 'update_time';// 更新时间字段

// 时间字段取出后的默认时间格式

protected $dateFormat = 'Y-m-d H:i:s';// 默认 的时间格式

// 字段类型或者格式转换

protected $type = [];// 字段类型 或者 格式转换

// 是否为更新数据

protected $isUpdate = false;// 是否为更新数据

// 更新条件

protected $updateWhere;//更新条件

// 当前执行的关联对象

protected $relation;// 当前执行的关联对象

// 验证失败是否抛出异常

protected $failException = false;// 是否抛出异常

// 全局查询范围

protected static $useGlobalScope = true;// 全局查询范围

/**

* 初始化过的模型.

*

* @var array

*/

protected static $initialized = [];// 模型初始化,看见这种配置,就会发现 更多的类型

/**

* 架构函数

* @access public

* @param array|object $data 数据

*/

public function __construct($data = [])// 架构函数

{

if (is_object($data)) {// 查看 传过来的 数据 是否为对象

$this->data = get_object_vars($data);// 对象方式获取  系统函数

} else {

$this->data = $data;

}

// 当前类名

$this->class = get_class($this);// 获取 类名 应该就不是 model 类 了,应该是继承他的类

if (empty($this->name)) {// 如果当前的模型名 为空

// 当前模型名

$this->name = basename(str_replace('\\', '/', $this->class));// 获取 类名 前面的部分 进行

}

if (is_null($this->autoWriteTimestamp)) {// 是否为空

// 自动写入时间戳

$this->autoWriteTimestamp = $this->db()->getConfig('auto_timestamp');// 大哥啊,你这个终于分开了,要不太费劲了

}

// 执行初始化操作

$this->initialize();// 进行 其它的初始化 操作

// 疑问? 抽象类 能被初始化吗? 好像可以的

}

/**

* 获取当前模型的数据库查询对象

* @access public

* @return Query

*/

public function db()

{

$model = $this->class;// 获取当前模型的数据库查询对象

if (!isset(self::$links[$model])) {// 如果 当前 连接的类 没有被 创建 连接对象

// 设置当前模型 确保查询返回模型对象

$query = Db::connect($this->connection)->model($model);// 创制连接

// 设置当前数据表和模型名

if (!empty($this->table)) {

$query->setTable($this->table);// 设置表名

} else {

$query->name($this->name);// 设置模型名

}

if (!empty($this->field)) {// 获取字段

if (true === $this->field) {// 如果 主键是true

$type = $this->db()->getTableInfo('', 'type');

} else {// 否则

$type = [];

foreach ((array) $this->field as $key => $val) {// 强转数组

if (is_int($key)) {// 字段类型 配置

$key = $val;

$val = 'varchar';

}

$type[$key] = $val;

}

}

$query->setFieldType($type);

$this->field = array_keys($type);

$query->allowField($this->field);

}

if (!empty($this->pk)) {// 获取主键

$query->pk($this->pk);

}

self::$links[$model] = $query;

}

// 返回当前模型的数据库查询对象

return self::$links[$model];

}

/**

*  获取关联模型实例

* @access protected

* @param string|array $relation 关联查询

* @return Relation|Query

*/

protected function relation($relation = null)

{

if (!is_null($relation)) {

// 执行关联查询

return $this->db()->relation($relation);

}

// 获取关联对象实例

if (is_null($this->relation)) {

$this->relation = new Relation($this);

}

return $this->relation;

}

/**

*  初始化模型

* @access protected

* @return void

*/

protected function initialize()

{

$class = get_class($this);

if (!isset(static::$initialized[$class])) {

static::$initialized[$class] = true;// static 应该是 继承当前抽象类实际类 被初始化

static::init();// 实际执行 当前的 数据

}

}

/**

* 初始化处理

* @access protected

* @return void

*/

protected static function init()

{}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值