<?php // +---------------------------------------------------------------------- // | 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 <[email protected]> // +---------------------------------------------------------------------- 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() {}
[李景山php]每天TP5-20170108|thinkphp5-Model.php-1
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.