<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ## 軟刪除:SoftDelete >[info] 軟刪除其實就是:偽刪除,沒有真正的刪除。正所謂,吃軟飯的都是偽男一樣的。 軟刪除是相對于硬刪除來說的,所謂“軟”是指刪除以后,還可以恢復的。而“硬刪除”則是物理刪除,是真正的將數據從硬盤上擦掉了。 * * * * * #### 1. 步驟:總的來說就二步 1. 在對應的模型類中做好相關的設置工作; 2. 在控制器中對該模型進行調用。 * * * * * #### 2. 源碼:/thinkphp/library/traits/model/SoftDelete.php >[info] 源碼中注釋是本人單獨添加,官方源碼中并無這么多的注釋 ~~~ <?php namespace traits\model; trait SoftDelete { /** * 判斷當前實例是否被軟刪除 * @access public * @return boolean */ public function trashed() { //獲取軟刪除字段名稱 $field = $this->getDeleteTimeField(); //如果當前數據對象的軟刪除字段為null,則已軟刪除 if (!empty($this->data[$field])) { return true; } //否則返回false,表示沒有被軟刪除 return false; } /** * 查詢軟刪除數據 * @access public * @return \think\db\Query */ public static function withTrashed() { //實例化靜態調用該方法的類,通常是子類:自定義的模型類,如Staff,User $model = new static(); //getDeleteTimeField(true)表示當前為讀操作:false表示寫操作 $field = $model->getDeleteTimeField(true); //在查結結果中,去掉已軟刪除的記錄 return $model->db(false)->removeWhereField($field); } /** * 只查詢軟刪除數據 * @access public * @return \think\db\Query */ public static function onlyTrashed() { //實例化靜態調用該方法的類 $model = new static(); //獲取軟刪除字段(true為讀操作) $field = $model->getDeleteTimeField(true); //只輸出刪除字段不為空的數據,其實就是已經軟刪除的記錄:理論上已刪除 return $model->db(false)->where($field, 'exp', 'is not null'); } /** * 刪除當前的記錄 * 此方法會覆蓋父類Model類中的delete方法,就是trait類的用處:多繼承 * @access public * @param bool $force 是否強制刪除 true:硬刪除 * @return integer */ public function delete($force = false) { if (false === $this->trigger('before_delete', $this)) { return false; } $name = $this->getDeleteTimeField(); if (!$force) { // 軟刪除 $this->change[] = $name; $this->data[$name] = $this->autoWriteTimestamp($name); $result = $this->isUpdate()->save(); } else { $result = $this->db()->delete($this->data); } $this->trigger('after_delete', $this); return $result; } /** * 刪除記錄/軟刪除 * 該方法同樣覆蓋父類Model同名方法,用于靜態調用 * @access public * @param mixed $data 主鍵列表 支持閉包查詢條件 * @param bool $force 是否強制刪除 * @return integer 成功刪除的記錄數 */ public static function destroy($data, $force = false) { //實例化當前調用類 $model = new static(); $query = $model->db(); if (is_array($data) && key($data) !== 0) { $query->where($data); $data = null; } elseif ($data instanceof \Closure) { call_user_func_array($data, [ & $query]); $data = null; } elseif (is_null($data)) { return 0; } $resultSet = $query->select($data); $count = 0; if ($resultSet) { foreach ($resultSet as $data) { $result = $data->delete($force); $count += $result; } } return $count; } /** * 恢復被軟刪除的記錄 * @access public * @param array $where 更新條件 * 參數為空,則恢復全部被軟刪除的數據 * @return integer */ public function restore($where = []) { $name = $this->getDeleteTimeField(); // 恢復刪除 return $this->isUpdate()->save([$name => null], $where); } /** * 查詢默認不包含軟刪除數據 * @access protected * @param \think\db\Query $query 查詢對象 * @return void */ protected function base($query) { $field = $this->getDeleteTimeField(true); $query->where($field, 'null'); } /** * 獲取軟刪除字段 * @access public * @param bool $read 是否查詢操作 寫操作的時候會自動去掉表別名 * @return string */ protected function getDeleteTimeField($read = false) { //獲取軟刪除字段名稱,如用戶沒有自定義,則設置成默認值:delete_time $field = isset($this->deleteTime) ? $this->deleteTime : 'delete_time'; //判斷字段名如果存在'.',則獲取表名,并添加到字段前 if (!strpos($field, '.')) { $field = $this->db(false)->getTable() . '.' . $field; } //寫操作時,去掉表名 if (!$read && strpos($field, '.')) { $array = explode('.', $field); $field = array_pop($array); } //返回軟刪除字段名稱 return $field; } } ~~~ >[warning] 閱讀源碼,發現有很多有用的方法,可以供模型類調用!其中的delete和destroy方法,直接攔截了調用Model類的對應方法,實現了軟刪除,理解這點很重要,這也是trait類的作用之一。 * * * * * #### 3. 方法總結: * 普通方法(public function ) | 序號 | 名稱 | 參數 | 返回值 | 功能 | | --- | --- | --- | --- | --- | | 1 | trashed | 無 | 布爾值 | 判斷當前實例是否被軟刪除 | | 2 | delete | 布爾值 | 刪除數量 | 刪除當前的數據對象 | | 3 | restore | 條件表達式 | 恢復數量 | 恢復被軟刪除的記錄 | * 靜態方法(public static) | 序號 | 名稱 | 參數 | 返回值 | 功能 | | --- | --- | --- | --- | --- | | 1 | withTrashed | 無 | Query對象 | 查詢軟刪除數據 | | 2 | onlyTrashed | 無 | Query對象 | 只查詢軟刪除數據 | | 3 | destroy | 主鍵/表達多/閉包 | 刪除數量 | 刪除數據 | * 保護方法(protected function) | 序號 | 名稱 | 參數 | 返回值 | 功能 | | --- | --- | --- | --- | --- | | 1 | base | Query對象 | 無定義 | 查詢默認不包含軟刪除數據 | | 2 | getDeleteTimeField| 布爾值 | 字段名稱 | 獲取軟刪除字段| * * * * * >[success] 下一節,我們以實例來詳細講解幾個常用方法~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看