|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Brett O'Donnell <[email protected]> |
|
4
|
|
|
* @copyright 2015 Mr PHP |
|
5
|
|
|
* @link https://github.com/cornernote/yii2-softdelete |
|
6
|
|
|
* @license BSD-3-Clause https://raw.github.com/cornernote/yii2-softdelete/master/LICENSE.md |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace cornernote\softdelete; |
|
10
|
|
|
|
|
11
|
|
|
use yii\base\Behavior; |
|
12
|
|
|
use yii\base\Event; |
|
13
|
|
|
use yii\db\BaseActiveRecord; |
|
14
|
|
|
use yii\db\Expression; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* SoftDeleteBehavior |
|
18
|
|
|
* |
|
19
|
|
|
* @usage: |
|
20
|
|
|
* ``` |
|
21
|
|
|
* public function behaviors() { |
|
22
|
|
|
* return [ |
|
23
|
|
|
* [ |
|
24
|
|
|
* 'class' => 'cornernote\behaviors\SoftDeleteBehavior', |
|
25
|
|
|
* 'attribute' => 'delete_time', |
|
26
|
|
|
* 'value' => new Expression('NOW()'), |
|
27
|
|
|
* ], |
|
28
|
|
|
* ]; |
|
29
|
|
|
* } |
|
30
|
|
|
* ``` |
|
31
|
|
|
* |
|
32
|
|
|
* @property BaseActiveRecord $owner |
|
33
|
|
|
*/ |
|
34
|
|
|
class SoftDeleteBehavior extends Behavior |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* @var string SoftDelete attribute |
|
38
|
|
|
*/ |
|
39
|
|
|
public $attribute = 'deleted_at'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var callable|Expression The expression that will be used for generating the timestamp. |
|
43
|
|
|
* This can be either an anonymous function that returns the timestamp value, |
|
44
|
|
|
* or an [[Expression]] object representing a DB expression (e.g. `new Expression('NOW()')`). |
|
45
|
|
|
* If not set, it will use the value of `time()` to set the attributes. |
|
46
|
|
|
*/ |
|
47
|
|
|
public $value; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @inheritdoc |
|
51
|
|
|
*/ |
|
52
|
10 |
|
public function events() |
|
53
|
|
|
{ |
|
54
|
10 |
|
return [BaseActiveRecord::EVENT_BEFORE_DELETE => 'softDeleteEvent']; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Set the attribute with the current timestamp to mark as deleted |
|
59
|
|
|
* |
|
60
|
|
|
* @param Event $event |
|
61
|
|
|
*/ |
|
62
|
4 |
|
public function softDeleteEvent($event) |
|
63
|
|
|
{ |
|
64
|
|
|
// remove and mark as invalid to prevent real deletion |
|
65
|
4 |
|
$this->softDelete(); |
|
66
|
4 |
|
$event->isValid = false; |
|
67
|
4 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Soft delete record |
|
71
|
|
|
*/ |
|
72
|
5 |
|
public function softDelete() |
|
73
|
|
|
{ |
|
74
|
|
|
// set attribute with evaluated timestamp |
|
75
|
5 |
|
$attribute = $this->attribute; |
|
76
|
5 |
|
$this->owner->$attribute = $this->getValue(null); |
|
77
|
|
|
// save record |
|
78
|
5 |
|
return $this->owner->save(false, [$attribute]); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Restore record |
|
83
|
|
|
*/ |
|
84
|
2 |
|
public function unDelete() |
|
85
|
|
|
{ |
|
86
|
|
|
// set attribute as null |
|
87
|
2 |
|
$attribute = $this->attribute; |
|
88
|
2 |
|
$this->owner->$attribute = null; |
|
89
|
|
|
// save record |
|
90
|
2 |
|
return $this->owner->save(false, [$attribute]); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Delete record from database regardless of the $safeMode attribute |
|
95
|
|
|
*/ |
|
96
|
2 |
|
public function hardDelete() |
|
97
|
|
|
{ |
|
98
|
|
|
// store model so that we can detach the behavior |
|
99
|
2 |
|
$model = $this->owner; |
|
100
|
2 |
|
$this->detach(); |
|
101
|
|
|
// delete as normal |
|
102
|
2 |
|
return 0 !== $model->delete(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Evaluate the timestamp to be saved. |
|
107
|
|
|
* |
|
108
|
|
|
* @param Event|null $event the event that triggers the current attribute updating. |
|
109
|
|
|
* @return mixed the attribute value |
|
110
|
|
|
*/ |
|
111
|
5 |
|
protected function getValue($event) |
|
112
|
|
|
{ |
|
113
|
5 |
|
if ($this->value instanceof Expression) { |
|
114
|
2 |
|
return $this->value; |
|
115
|
|
|
} else { |
|
116
|
3 |
|
return $this->value !== null ? call_user_func($this->value, $event) : time(); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|