標籤:thinkphp
/** * 設定需要追加的輸出屬性 * @access public * @param array $append 屬性列表 * @return $this */public function append($append = []){ $this->append = $append;// 屬性追加 return $this;}/** * 設定需要隱藏的輸出屬性 * @access public * @param array $hidden 屬性列表 * @return $this */public function hidden($hidden = []){ $this->hidden = $hidden;// 屬性隱藏 return $this;}/** * 設定需要輸出的屬性 * @param array $visible * @return $this */public function visible($visible = []){ $this->visible = $visible;// 輸出的屬性 return $this;}/** * 轉換當前模型對象為數組 * @access public * @return array */public function toArray(){ $item = []; //過濾屬性 if (!empty($this->visible)) { $data = array_intersect_key($this->data, array_flip($this->visible)); } elseif (!empty($this->hidden)) { $data = array_diff_key($this->data, array_flip($this->hidden)); } else { $data = $this->data; } foreach ($data as $key => $val) { if ($val instanceof Model || $val instanceof Collection) { // 關聯模型對象 $item[$key] = $val->toArray(); } elseif (is_array($val) && reset($val) instanceof Model) { // 關聯模型資料集 $arr = []; foreach ($val as $k => $value) { $arr[$k] = $value->toArray(); } $item[$key] = $arr; } else { // 模型屬性 $item[$key] = $this->getAttr($key); } } // 追加屬性(必須定義擷取器) if (!empty($this->append)) { foreach ($this->append as $name) { $item[$name] = $this->getAttr($name); } } return !empty($item) ? $item : [];}// 轉換為數組/** * 轉換當前模型對象為JSON字串 * @access public * @param integer $options json參數 * @return string */public function toJson($options = JSON_UNESCAPED_UNICODE){ return json_encode($this->toArray(), $options);// 轉換wield json}/** * 擷取模型對象的主鍵 * @access public * @param string $name 模型名 * @return mixed */public function getPk($name = ‘‘){// 擷取主鍵 if (!empty($name)) { $table = $this->db()->getTable($name); return $this->db()->getPk($table); } elseif (empty($this->pk)) { $this->pk = $this->db()->getPk(); } return $this->pk;}/** * 判斷一個欄位名是否為主鍵欄位 * @access public * @param string $key 名稱 * @return bool */protected function isPk($key){// 檢測是否為主鍵 $pk = $this->getPk(); if (is_string($pk) && $pk == $key) { return true; } elseif (is_array($pk) && in_array($key, $pk)) { return true; } return false;}/** * 儲存當前資料對象 * @access public * @param array $data 資料 * @param array $where 更新條件 * @param string $sequence 自增序列名 * @return integer|false */public function save($data = [], $where = [], $sequence = null){// 資料儲存 if (!empty($data)) {// 資料不為空白 // 資料自動驗證 if (!$this->validateData($data)) { return false; } // 資料對象賦值 foreach ($data as $key => $value) {// 資料對象賦值 $this->setAttr($key, $value, $data); } if (!empty($where)) {// 條件不為空白 代表為 更新狀態 $this->isUpdate = true; } } // 檢測欄位 if (!empty($this->field)) {// 欄位不為空白 $this->db(); foreach ($this->data as $key => $val) {// 刪除資料裡面的 多有的欄位資訊 if (!in_array($key, $this->field)) { unset($this->data[$key]); } } } // 資料自動完成 $this->autoCompleteData($this->auto);// 資料自動完成 // 自動寫入更新時間 if ($this->autoWriteTimestamp && $this->updateTime) {// 時間屬性設定 $this->setAttr($this->updateTime, null); } // 事件回調 if (false === $this->trigger(‘before_write‘, $this)) {// 回調 return false; } if ($this->isUpdate) {// 執行更新 // 自動更新 $this->autoCompleteData($this->update); // 事件回調 if (false === $this->trigger(‘before_update‘, $this)) { return false; } // 去除沒有更新的欄位 $data = []; foreach ($this->data as $key => $val) { if (in_array($key, $this->change) || $this->isPk($key)) { $data[$key] = $val; } } if (!empty($this->readonly)) { // 唯讀欄位不允許更新 foreach ($this->readonly as $key => $field) { if (isset($data[$field])) { unset($data[$field]); } } } if (empty($where) && !empty($this->updateWhere)) { $where = $this->updateWhere; } if (!empty($where)) { $pk = $this->getPk(); if (is_string($pk) && isset($data[$pk])) { unset($data[$pk]); } } $result = $this->db()->where($where)->update($data);// 還是使用資料庫 模型的更新 // 清空change $this->change = []; // 更新回調 $this->trigger(‘after_update‘, $this); } else { // 自動寫入 $this->autoCompleteData($this->insert); // 自動寫入建立時間 if ($this->autoWriteTimestamp && $this->createTime) { $this->setAttr($this->createTime, null); } if (false === $this->trigger(‘before_insert‘, $this)) { return false; } $result = $this->db()->insert($this->data);// 資料模型的插入 // 擷取自動成長主鍵 if ($result) { $insertId = $this->db()->getLastInsID($sequence);// 擷取自動成長的主鍵 $pk = $this->getPk(); if (is_string($pk) && $insertId) { $this->data[$pk] = $insertId; } } // 標記為更新 $this->isUpdate = true; // 清空change $this->change = []; // 新增回調 $this->trigger(‘after_insert‘, $this); } // 寫入回調 $this->trigger(‘after_write‘, $this); return $result;}/** * 儲存多個資料到當前資料對象 * @access public * @param array $dataSet 資料 * @param boolean $replace 是否自動識別更新和寫入 * @return array|false */public function saveAll($dataSet, $replace = true)// 基於事務的 批量插入及更新{ if ($this->validate) {//批量資料驗證 // 資料批量驗證 $validate = $this->validate; foreach ($dataSet as $data) { if (!$this->validate($validate)->validateData($data)) { return false; } } } $result = []; $db = $this->db(); $db->startTrans();// 啟動事務 try { $pk = $this->getPk(); if (is_string($pk) && $replace) { $auto = true; } foreach ($dataSet as $key => $data) { if (!empty($auto) && isset($data[$pk])) { $result[$key] = self::update($data); } else { $result[$key] = self::create($data); } } $db->commit();// 提交事務 return $result; } catch (\Exception $e) { $db->rollback();// 否則復原 throw $e; }}
本文出自 “專註php 群號:414194301” 部落格,請務必保留此出處http://jingshanls.blog.51cto.com/3357095/1880589
[李景山php]每天TP5-20170111|thinkphp5-Model.php-4