標籤:thinkphp
/** * 設定允許寫入的欄位 * @access public * @param bool|array $field 允許寫入的欄位 如果為true只允許寫入資料表欄位 * @return $this */public function allowField($field){// 關於資料庫的操作,設定許可欄位 if (true === $field) {// 如果欄位為 true $field = $this->db()->getTableInfo(‘‘, ‘type‘);// 擷取 表欄位 資訊 $this->db()->setFieldType($field);// 設定 欄位類型 $field = array_keys($field);// 返回 欄位資訊 } $this->field = $field;// 否則 擷取欄位 設定欄位資訊到當前模型 return $this;// 返回對象本身}/** * 是否為更新資料 * @access public * @param bool $update * @param mixed $where * @return $this */public function isUpdate($update = true, $where = null){// 是否為 更新 資料 $this->isUpdate = $update;// 預設 是 true if (!empty($where)) { $this->updateWhere = $where; // 如果沒有 條件 就不是更新語句 } return $this;// 返回對象本身}/** * 資料自動完成 * @access public * @param array $auto 要自動更新的欄位列表 * @return void */protected function autoCompleteData($auto = []){// 資料自動完成 自動完成資料傳入 foreach ($auto as $field => $value) {// 迴圈 if (is_integer($field)) {// 如果欄位是整型 $field = $value;// 賦值欄位 過來 $value = null;// 賦值為空白 } if (!in_array($field, $this->change)) {// 如果欄位 在改變範圍內,使用改變函數 設定 $this->setAttr($field, !is_null($value) ? $value : (isset($this->data[$field]) ? $this->data[$field] : $value)); } }}/** * 刪除當前的記錄 * @access public * @return integer */public function delete(){// 分別形成了,對 db 的二次 封裝 if (false === $this->trigger(‘before_delete‘, $this)) {// 啟動之前的東西 return false; } $result = $this->db()->delete($this->data);// 刪除 $this->trigger(‘after_delete‘, $this);// 刪除之後的東西 return $result;// 返回刪除結果}/** * 設定自動完成的欄位( 規則通過修改器定義) * @access public * @param array $fields 需要自動完成的欄位 * @return $this */public function auto($fields)// 自動完成 欄位{ $this->auto = $fields;// 欄位 自動完成的 欄位 return $this;}/** * 設定欄位驗證 * @access public * @param array|string|bool $rule 驗證規則 true表示自動讀取驗證器類 * @param array $msg 提示資訊 * @return $this */public function validate($rule = true, $msg = [])// 設定欄位 驗證{ if (is_array($rule)) {// 設定 自動完成的規則 $this->validate = [ ‘rule‘ => $rule, ‘msg‘ => $msg, ]; } else { $this->validate = true === $rule ? $this->name : $rule;// 自動完成 欄位 } return $this;}/** * 設定驗證失敗後是否拋出異常 * @access public * @param bool $fail 是否拋出異常 * @return $this */public function validateFailException($fail = true){ $this->failException = $fail;// 是否 異常 拋出 return $this;}/** * 自動驗證資料 * @access protected * @param array $data 驗證資料 * @return bool */protected function validateData($data)// 自動驗證資料{ if (!empty($this->validate)) {// 如果規則為空白 直接飄過,否則 $info = $this->validate;// 擷取資訊 if (is_array($info)) {// 數組 $validate = Loader::validate();// 載入自動驗證類 $validate->rule($info[‘rule‘]);// 設定規則 $validate->message($info[‘msg‘]);// 設定資訊 } else { $name = is_string($info) ? $info : $this->name;//單欄位 驗證, if (strpos($name, ‘.‘)) { list($name, $scene) = explode(‘.‘, $name); } $validate = Loader::validate($name); if (!empty($scene)) { $validate->scene($scene);// 特殊的處理用法 } } if (!$validate->check($data)) {// 資料效驗 結果 $this->error = $validate->getError();// 沒通過效驗的錯誤資訊 if ($this->failException) {// 如果允許拋出異常,拋出異常 throw new ValidateException($this->error); } else { return false; } } $this->validate = null;// 清空 驗證規則 } return true;}/** * 返回模型的錯誤資訊 * @access public * @return string */public function getError()// 返回錯誤資訊{ return $this->error;}/** * 註冊回調方法 * @access public * @param string $event 事件名 * @param callable $callback 回調方法 * @param bool $override 是否覆蓋 * @return void */public static function event($event, $callback, $override = false){ $class = get_called_class();// 擷取回調類 if ($override) { self::$event[$class][$event] = []; } self::$event[$class][$event][] = $callback;// 事件回調方法}/** * 觸發事件 * @access protected * @param string $event 事件名 * @param mixed $params 傳入參數(引用) * @return bool */protected function trigger($event, &$params)// 觸發相應的事件{ if (isset(self::$event[$this->class][$event])) {// 擷取類對應的 時間 函數 foreach (self::$event[$this->class][$event] as $callback) { if (is_callable($callback)) {// 可以調用 $result = call_user_func_array($callback, [ & $params]);// 調用結果 if (false === $result) { return false;// 如果其中一個終止了這個事情,停止繼續執行 } } } } return true;}/** * 寫入資料 * @access public * @param array $data 資料數組 * @return $this */public static function create($data = []){ $model = new static(); $model->isUpdate(false)->save($data, []);// 進行資料寫入,通過model 寫入,還是很厲害啊 return $model;}/** * 更新資料 * @access public * @param array $data 資料數組 * @param array $where 更新條件 * @return $this */public static function update($data = [], $where = []){ $model = new static(); $result = $model->isUpdate(true)->save($data, $where);// 資料 條件 return $model;}/** * 尋找單條記錄 * @access public * @param mixed $data 主索引值或者查詢條件(閉包) * @param array|string $with 關聯預查詢 * @param bool $cache 是否緩衝 * @return static * @throws exception\DbException */public static function get($data = null, $with = [], $cache = false){ $query = static::parseQuery($data, $with, $cache); return $query->find($data);//單條記錄}/** * 尋找所有記錄 * @access public * @param mixed $data 主鍵列表或者查詢條件(閉包) * @param array|string $with 關聯預查詢 * @param bool $cache 是否緩衝 * @return static[]|false * @throws exception\DbException */public static function all($data = null, $with = [], $cache = false){ $query = static::parseQuery($data, $with, $cache); return $query->select($data);// 多條資料}
本文出自 “專註php 群號:414194301” 部落格,請務必保留此出處http://jingshanls.blog.51cto.com/3357095/1881007
[李景山php]每天TP5-20170112|thinkphp5-Model.php-5