|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ronanchilvers\Orm\Features; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Trait for managing timestamps on a model |
|
10
|
|
|
* |
|
11
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
trait HasTimestamps |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var boolean |
|
17
|
|
|
*/ |
|
18
|
|
|
static protected $usesTimestamps = true; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
static protected $created = 'created'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
static protected $updated = 'updated'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Boot the timestamps for this model |
|
32
|
|
|
* |
|
33
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
34
|
|
|
*/ |
|
35
|
2 |
|
protected function bootHasTimestamps() |
|
36
|
|
|
{ |
|
37
|
2 |
|
$this->addType('datetime', static::$created); |
|
38
|
2 |
|
$this->addType('datetime', static::$updated); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Does this model use timestamps? |
|
43
|
|
|
* |
|
44
|
|
|
* @return boolean |
|
45
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
46
|
|
|
*/ |
|
47
|
2 |
|
protected function useTimestamps() |
|
48
|
|
|
{ |
|
49
|
2 |
|
return static::$usesTimestamps; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Update the timestamps for this model |
|
54
|
|
|
* |
|
55
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function updateTimestamps() |
|
58
|
|
|
{ |
|
59
|
|
|
$now = Carbon::now(); |
|
60
|
|
|
if (!$this->isLoaded() && !$this->isDirty(static::$created)) { |
|
|
|
|
|
|
61
|
|
|
$this->setAttribute(static::$created, $now); |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
if ($this->isLoaded() && !$this->isDirty(static::$updated)) { |
|
64
|
|
|
$this->setAttribute(static::$updated, $now); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Clear the timestamps on this model |
|
70
|
|
|
* |
|
71
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function clearTimestamps() |
|
74
|
|
|
{ |
|
75
|
|
|
$this->setAttribute(static::$created, null); |
|
76
|
|
|
$this->setAttribute(static::$updated, null); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|