From 9f0fa242e6fd35ec4873cec5f0b2c3ec85442082 Mon Sep 17 00:00:00 2001 From: Deon George Date: Fri, 11 Nov 2022 23:04:28 +1100 Subject: [PATCH] When deleting an Address change active to FALSE --- app/Jobs/NodelistImport.php | 5 ++++- app/Models/Address.php | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/Jobs/NodelistImport.php b/app/Jobs/NodelistImport.php index cc458c6..ab6facb 100644 --- a/app/Jobs/NodelistImport.php +++ b/app/Jobs/NodelistImport.php @@ -373,7 +373,10 @@ class NodelistImport implements ShouldQueue } // Remove addresses not recorded; - Address::whereIN('id',$zo->addresses->except($us->system->addresses->pluck('id')->toArray())->diff($no->addresses)->pluck('id')->toArray())->delete(); + $remove = $zo->addresses->except($us->system->addresses->pluck('id')->toArray())->diff($no->addresses); + Log::notice(sprintf('%s:Deleting addresses [%s]',self::LOGKEY,$remove->pluck('ftn2d')->join(','))); + Address::whereIN('id',$remove->pluck('id')->toArray())->update(['active'=>FALSE]); + Address::whereIN('id',$remove->pluck('id')->toArray())->delete(); if (crc16(substr($tocrc,0,-3)) == $file_crc) DB::commit(); diff --git a/app/Models/Address.php b/app/Models/Address.php index fb00c56..9cc7e9e 100644 --- a/app/Models/Address.php +++ b/app/Models/Address.php @@ -46,6 +46,19 @@ class Address extends Model public const NODE_POINT = 1<<8; // Point public const NODE_UNKNOWN = 1<<15; // Unknown + public static function boot() + { + parent::boot(); + + // For indexes when deleting, we need to change active to FALSE + static::softDeleted(function($model) { + Log::debug(sprintf('%s:Deleting [%d], updating active to FALSE',self::LOGKEY,$model->id)); + + $model->active = FALSE; + $model->save(); + }); + } + /* SCOPES */ public function scopeFTNOrder($query)