佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > Laravel 實現(xiàn)數(shù)據(jù)軟刪除功能

Laravel 實現(xiàn)數(shù)據(jù)軟刪除功能

熱門標(biāo)簽:電子圍欄 Mysql連接數(shù)設(shè)置 服務(wù)器配置 科大訊飛語音識別系統(tǒng) 團購網(wǎng)站 Linux服務(wù)器 阿里云 銀行業(yè)務(wù)

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

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

2.查詢的時候自動過濾已經(jīng)標(biāo)記為刪除的數(shù)據(jù)

3.可以設(shè)置是否查詢已刪除的數(shù)據(jù),可以設(shè)置只查詢已刪除的數(shù)據(jù)

4.已刪除數(shù)據(jù)可以恢復(fù)

Model的軟刪除功能實現(xiàn)

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 方法更新刪除標(biāo)記。

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查詢過濾刪除數(shù)據(jù)

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

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

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

遠程關(guān)聯(lián)數(shù)據(jù)的軟刪除處理

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

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 中通過中間模型關(guān)聯(lián)遠程模型,會根據(jù) throughParentSoftDeletes 判斷中間模型是否有軟刪除,如果有軟刪除會過濾掉中間模型被刪除的數(shù)據(jù)。

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

但是有的時候,項目中會使用一個整形的字段標(biāo)記數(shù)據(jù)是否刪除。在這樣的場景下,需要對Laravel的軟刪除進行修改才能夠?qū)崿F(xiàn)。

主要的方案是:

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

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

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

總結(jié)

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

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

標(biāo)簽:衡水 衢州 萍鄉(xiāng) 蚌埠 廣元 江蘇 大理 棗莊

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Laravel 實現(xiàn)數(shù)據(jù)軟刪除功能》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    垦利县| 吕梁市| 克拉玛依市| 灌阳县| 苍山县| 宁国市| 新民市| 利川市| 原平市| 革吉县| 福泉市| 黎川县| 元氏县| 福州市| 金川县| 报价| 台州市| 深圳市| 墨竹工卡县| 双牌县| 江安县| 台东市| 香港 | 隆林| 龙游县| 阳信县| 庆元县| 宣恩县| 英超| 中牟县| 安国市| 云梦县| 台东市| 辰溪县| 昌都县| 新乐市| 资溪县| 云阳县| 肇庆市| 丹江口市| 仁怀市|