<?php
// Example usage in a model
class YourModel extends BaseModel {
// ... your model attributes and settings ...
/* ----------------------------------*
* GARBAGE COLLECTOR VARIABLES
* --------------------------------- */
/**
* * GC-MODES:
*
* * Mode 1: default "deleted_at" Column from CI4-Models
* * Mode 2: Custom Delted_at-Column ($gc_deletedField)
* * Mode 3: Custom Deleted_at-Column, ($gc_deletedField) is timestamp (no Y-m-d H:i:s)
*/
protected $gc_name = "myCollectorName";
protected $gc_enabled = true;
protected $gc_mode = 1;
protected $gc_deletedField = 'model';
protected $gc_setDeletedOnExpired = true;
protected $gc_deleteExpiredAfterXDays = 14;
protected $gc_deleteAfterXDays = 60;
// ... other settings ...
// ... your model code ...
}
class BaseModel {
// ... your model attributes and settings ...
// ... your base model code ...
/* ----------------------------------*
* GARBAGE COLLECTOR FUNCTIONS
* --------------------------------- */
public function garbageCleaner(){
if($this->gc_enabled){
if($this->gc_setDeletedOnExpired){
$this->prepareForCleanUp();
}
return $this->executeCleanUp($this->gc_mode);
}
return true;
}
public function executeCleanUp($gc_mode){
if($gc_mode === 1){
$rowsToDelete = $this->getDeletedEntries($this->gc_deleteAfterXDays);
}else if($gc_mode === 2){
$rowsToDelete = $this->getDeletedEntries($this->gc_deleteAfterXDays, $this->gc_deletedField);
}else if($gc_mode === 3){
$rowsToDelete = $this->getDeletedEntries($this->gc_deleteAfterXDays, $this->gc_deletedField, true);
}else{
return false;
}
$count = count($rowsToDelete);
if($count > 0){
$idsToDelete = array_column($rowsToDelete, $this->primaryKey);
if($this->delete($idsToDelete, true)){
/* TODO: Insert your Log-Routine */
writeLog("$this->gc_name.$this->table", "deleted data | $count rows deleted");
}
}
return true;
}
public function prepareForCleanUp() {
$today = date($this->defaultTimeFormat);
$expiredRows = $this->getExpiredEntries($this->gc_deleteExpiredAfterXDays);
$count = count($expiredRows);
if($count > 0){
foreach($expiredRows as $key => $value){
$expiredRows[$key]->{$this->deletedField} = $today;
}
$this->updateBatch(
$expiredRows,
$this->primaryKey
);
/* TODO: Insert your Log-Routine */
writeLog("$this->gc_name.$this->table", "prepared expired data for delete | $count rows prepared");
}
return true;
}
protected function getDeletedEntries(int $olderThanXDays = 0, string $deletedField = 'model', bool $useTimestamp = false) {
if ($deletedField == 'model') {
$deletedField = $this->deletedField;
}
if ($olderThanXDays > 0) {
if ($useTimestamp) {
// Working with timestamps
$this->where("$deletedField + ($olderThanXDays * 24 * 3600) <= UNIX_TIMESTAMP(NOW())", null, false);
} else {
// Working with datetime
$this->where("DATE_ADD($deletedField, INTERVAL $olderThanXDays DAY) <= NOW()");
}
}
return $this->where($deletedField . ' IS NOT NULL')
->withDeleted()
->findAll();
}
protected function getExpiredEntries(int $olderThanXDays = 0, string $expiredField = 'model'){
if($expiredField == 'model'){
$expiredField = $this->expiredField;
}
if($olderThanXDays > 0){
$expirationDate = date($this->defaultTimeFormat, strtotime("+" . $olderThanXDays . " days"));
$this->where($expiredField . ' <', $expirationDate);
}
return $this->where($expiredField . ' IS NOT NULL')->findAll();
}
}