|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yeelight\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Yeelight\Models\Observers\BaseModelObserver; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Whenever a new model is saved for the first time, |
|
10
|
|
|
* the creating and created events will fire. |
|
11
|
|
|
* If a model already existed in the database and the save method is called, |
|
12
|
|
|
* the updating / updated events will fire. |
|
13
|
|
|
* However, in both cases, the saving / saved events will fire. |
|
14
|
|
|
* |
|
15
|
|
|
* @CREATE: saving > creating > created > saved |
|
16
|
|
|
* @UPDATE: saving > updating > updated > saved |
|
17
|
|
|
*/ |
|
18
|
|
|
trait BaseModelEvents |
|
19
|
|
|
{ |
|
20
|
|
|
protected static function boot() |
|
21
|
|
|
{ |
|
22
|
|
|
parent::boot(); |
|
23
|
|
|
|
|
24
|
|
|
/** @var Model $ModelName */ |
|
25
|
|
|
$ModelName = get_called_class(); |
|
26
|
|
|
|
|
27
|
|
|
// Setup event bindings... |
|
28
|
|
|
$ModelName::observe(new BaseModelObserver()); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function onCreating() |
|
32
|
|
|
{ |
|
33
|
|
|
|
|
34
|
|
|
// auto set user id |
|
35
|
|
|
if ($this->autoUserId && empty($this->user_id)) { |
|
36
|
|
|
$user_id = $this->getAuthUserId(); |
|
|
|
|
|
|
37
|
|
|
if ($user_id > 0) { |
|
38
|
|
|
$this->user_id = $user_id; |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function onCreated() |
|
44
|
|
|
{ |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function onUpdating() |
|
48
|
|
|
{ |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function onUpdated() |
|
52
|
|
|
{ |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function onSaving() |
|
56
|
|
|
{ |
|
57
|
|
|
|
|
58
|
|
|
// update ips if true |
|
59
|
|
|
if ($this->ips) { |
|
60
|
|
|
$this->updateIps(); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
// update users if true |
|
64
|
|
|
if ($this->update_users) { |
|
65
|
|
|
$this->updateUsers(); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function onSaved() |
|
70
|
|
|
{ |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function onDeleting() |
|
74
|
|
|
{ |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function onDeleted() |
|
78
|
|
|
{ |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function onRestoring() |
|
82
|
|
|
{ |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function onRestored() |
|
86
|
|
|
{ |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|