婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > Laravel 實現數據軟刪除功能

Laravel 實現數據軟刪除功能

熱門標簽:江西手機自動外呼防封系統是什么 怎么向銷售公司推銷外呼系統 高德地圖標注家 廣東地市地圖標注 廣州防封卡外呼系統多少錢一個月 外呼系統撥打暫時無法接通 哪里辦理400電話 長春人工外呼系統服務商 仁和怎么申請400開頭的電話

對于任何一個模型,如果需要使用軟刪除功能,需要在模型中使用 Illuminate\Database\Eloquent\SoftDeletes 這個  trait 。軟刪除功能需要實現的功能有以下幾點:

1.模型執(zhí)行刪除操作,只標記刪除,不執(zhí)行真正的數據刪除

2.查詢的時候自動過濾已經標記為刪除的數據

3.可以設置是否查詢已刪除的數據,可以設置只查詢已刪除的數據

4.已刪除數據可以恢復

Model的軟刪除功能實現

Illuminate\Database\Eloquent\Model 中delete方法源碼:

public function delete()
{
 if (is_null($this->getKeyName())) {
  throw new Exception('No primary key defined on model.');
 }
 if (! $this->exists) {
  return;
 }
 if ($this->fireModelEvent('deleting') === false) {
  return false;
 }
 $this->touchOwners();
 $this->performDeleteOnModel();
 $this->fireModelEvent('deleted', false);
 return true;
}
protected function performDeleteOnModel()
{
 $this->setKeysForSaveQuery($this->newModelQuery())
 ->delete();
 $this->exists = false;
}

因為在子類中使用了 SoftDeletes trait,所以, SoftDeletes performDeleteOnModel 方法會覆蓋父類的方法,最終通過  runSoftDelete 方法更新刪除標記。

protected function performDeleteOnModel()
{
 if ($this->forceDeleting) {
  $this->exists = false;
  return $this->newModelQuery()->where(
    $this->getKeyName(), $this->getKey()
  )->forceDelete();
 }
 return $this->runSoftDelete();
}

protected function runSoftDelete()
{
 $query = $this->newModelQuery()
      ->where($this->getKeyName(), $this->getKey());
 $time = $this->freshTimestamp();
 $columns = [$this->getDeletedAtColumn() => $this->fromDateTime($time)];
 $this->{$this->getDeletedAtColumn()} = $time;
 if ($this->timestamps  ! is_null($this->getUpdatedAtColumn())) {
  $this->{$this->getUpdatedAtColumn()} = $time;
  $columns[$this->getUpdatedAtColumn()] = $this->fromDateTime($time);
 }
 $query->update($columns);
}

Model查詢過濾刪除數據

Laravel中允許在Model中 static::addGlobalScope 方法添加全局的 Scope 。這樣就可以在查詢條件中添加一個全局條件。Laravel中軟刪除數據的過濾也是使用這種方式實現的。

SoftDeletes trait中加入了 Illuminate\Database\Eloquent\SoftDeletingScope 全局的 Scope 。并在 SoftDeletingScope 中實現查詢自動過濾被刪除數據,指定查詢已刪除數據功能。

public static function bootSoftDeletes()
{
 static::addGlobalScope(new SoftDeletingScope);
}

遠程關聯數據的軟刪除處理

Scope的作用只在于當前模型,以及關聯模型操作上。如果是遠程關聯,則還需要額外的處理。Laravel遠程關聯關系通過 hasManyThrough 實現。里面有兩個地方涉及到軟刪除的查詢。

protected function performJoin(Builder $query = null)
{
 $query = $query ?: $this->query;
 $farKey = $this->getQualifiedFarKeyName();
 $query->join($this->throughParent->getTable(), $this->getQualifiedParentKeyName(), '=', $farKey);
 if ($this->throughParentSoftDeletes()) {
  $query->whereNull(
   $this->throughParent->getQualifiedDeletedAtColumn()
  );
 }
}

public function throughParentSoftDeletes()
{
 return in_array(SoftDeletes::class, class_uses_recursive(
  get_class($this->throughParent)
 ));
}
public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*'])
{
 $query->from( $query->getModel()->getTable().' as '
  .$hash = $this->getRelationCountHash()
 );
 $query->join($this->throughParent->getTable(), 
  $this->getQualifiedParentKeyName(), '=', $hash.'.'.$this->secondLocalKey
 );
 if ($this->throughParentSoftDeletes()) {
  $query->whereNull($this->throughParent->getQualifiedDeletedAtColumn());
 }
 $query->getModel()->setTable($hash);
 return $query->select($columns)->whereColumn(
  $parentQuery->getQuery()->from.'.'.$query->getModel()->getKeyName(), '=', $this->getQualifiedFirstKeyName()
 );
}

performJoin 中通過中間模型關聯遠程模型,會根據 throughParentSoftDeletes 判斷中間模型是否有軟刪除,如果有軟刪除會過濾掉中間模型被刪除的數據。

以上就是Laravel實現軟刪除的大概邏輯。這里有一個細節(jié),Laravel中軟刪除的標記是一個時間格式的字段,默認 delete_at 。通過是否為null判斷數據是否刪除。

但是有的時候,項目中會使用一個整形的字段標記數據是否刪除。在這樣的場景下,需要對Laravel的軟刪除進行修改才能夠實現。

主要的方案是:

1.自定義 SoftDeletes trait,修改字段名稱,修改更新刪除標記操作;

2.自定義 SoftDeletingScope 修改查詢條件

3.自定義 HasRelationships trait,在自定義的 HasRelationships 中重寫 newHasManyThrough 方法,實例化自定義的 HasManyThrough 對象

總結

以上所述是小編給大家介紹的Laravel 實現數據軟刪除功能,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!

您可能感興趣的文章:
  • Laravel5.1 框架模型創(chuàng)建與使用方法實例分析
  • Laravel 5框架學習之模型、控制器、視圖基礎流程
  • Laravel模型事件的實現原理詳解
  • Laravel模型間關系設置分表的方法示例
  • laravel學習教程之關聯模型
  • laravel學習筆記之模型事件的幾種用法示例
  • Laravel框架模型的創(chuàng)建及模型對數據操作示例
  • laravel model模型處理之修改查詢或修改字段時的類型格式案例
  • Laravel 關聯模型-關聯新增和關聯更新的方法
  • Laravel 模型使用軟刪除-左連接查詢-表起別名示例
  • Laravel5.1 框架模型軟刪除操作實例分析

標簽:廈門 惠州 黔東 文山 梅河口 海北 濮陽 湘西

巨人網絡通訊聲明:本文標題《Laravel 實現數據軟刪除功能》,本文關鍵詞  Laravel,實現,數據,軟,刪除,;如發(fā)現本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Laravel 實現數據軟刪除功能》相關的同類信息!
  • 本頁收集關于Laravel 實現數據軟刪除功能的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 县级市| 阿勒泰市| 洛阳市| 东海县| 五寨县| 响水县| 增城市| 许昌县| 泗洪县| 科技| 阿瓦提县| 游戏| 右玉县| 茶陵县| 永德县| 阿瓦提县| 蒙自县| 临泽县| 枝江市| 马鞍山市| 沈阳市| 江华| 孟津县| 泉州市| 寻乌县| 双辽市| 绥中县| 泰顺县| 峡江县| 天全县| 婺源县| 闵行区| 孟连| 九龙县| 磴口县| 崇左市| 康马县| 佛冈县| 沁阳市| 毕节市| 如东县|