標籤:thinkphp5
<?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;// 命名空間 跟目錄 thinkuse 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 群號:414194301” 部落格,請務必保留此出處http://jingshanls.blog.51cto.com/3357095/1879464
[李景山php]每天TP5-20170108|thinkphp5-Model.php-1