-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrunable.php
More file actions
65 lines (59 loc) · 1.64 KB
/
Copy pathPrunable.php
File metadata and controls
65 lines (59 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
namespace MJ\WPORM;
/**
* Trait for pruning old model records.
*
* Add this trait to a model and define a prunable() method that returns
* a query builder constraining which records should be removed. The
* prune() static method will delete matching records one at a time,
* firing model events for each deletion.
*
* Usage:
* class AuditLog extends Model {
* use Prunable;
*
* public function prunable() {
* return static::query()->where('created_at', '<', now()->subDays(90));
* }
* }
*
* // Run the pruning
* AuditLog::prune();
*
* @see \MJ\WPORM\MassPrunable for chunk-based bulk pruning
*/
trait Prunable
{
/**
* Get the query builder that defines which records to prune.
*
* Must return a QueryBuilder instance. The trait will call delete()
* on the result, processing one record at a time with model events.
*
* @return \MJ\WPORM\QueryBuilder
*/
abstract public function prunable();
/**
* Prune old records matching the prunable() query.
*
* Processes records one at a time, firing model events (deleting/deleted)
* for each. Returns the total number of records pruned.
*
* @return int Number of records pruned
*/
public static function prune(): int
{
$instance = new static;
$query = $instance->prunable();
if (!($query instanceof \MJ\WPORM\QueryBuilder)) {
return 0;
}
$count = 0;
foreach ($query->cursor() as $model) {
if ($model->delete() !== false) {
$count++;
}
}
return $count;
}
}