|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace miBadger\ActiveRecord\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use miBadger\Query\Query; |
|
6
|
|
|
use miBadger\ActiveRecord\ColumnProperty; |
|
7
|
|
|
|
|
8
|
|
|
const TRAIT_DATEFIELDS_LAST_MODIFIED = "last_modified"; |
|
9
|
|
|
const TRAIT_DATEFIELDS_CREATED = "created"; |
|
10
|
|
|
|
|
11
|
|
|
trait Datefields |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var string The timestamp representing the moment this record was created */ |
|
14
|
|
|
protected $created; |
|
15
|
|
|
|
|
16
|
|
|
/** @var string The timestamp representing the moment this record was last updated */ |
|
17
|
|
|
protected $lastModified; |
|
18
|
|
|
|
|
19
|
|
|
protected function initDatefields() |
|
20
|
|
|
{ |
|
21
|
|
|
$this->extendTableDefinition(TRAIT_DATEFIELDS_CREATED, [ |
|
|
|
|
|
|
22
|
|
|
'value' => &$this->created, |
|
23
|
|
|
'validate' => null, |
|
24
|
|
|
'type' => 'DATETIME', |
|
25
|
|
|
'default' => 'CURRENT_TIMESTAMP', |
|
26
|
|
|
'properties' => ColumnProperty::NOT_NULL | ColumnProperty::IMMUTABLE |
|
27
|
|
|
]); |
|
28
|
|
|
|
|
29
|
|
|
$this->extendTableDefinition(TRAIT_DATEFIELDS_LAST_MODIFIED, [ |
|
|
|
|
|
|
30
|
|
|
'value' => &$this->lastModified, |
|
31
|
|
|
'validate' => null, |
|
32
|
|
|
'type' => 'DATETIME', |
|
33
|
|
|
'default' => 'CURRENT_TIMESTAMP', |
|
34
|
|
|
// 'on_update' => 'CURRENT_TIMESTAMP', // @TODO(Discuss): Should we support this? (would not sync with object) |
|
35
|
|
|
'properties' => ColumnProperty::NOT_NULL | ColumnProperty::IMMUTABLE |
|
36
|
|
|
]); |
|
37
|
|
|
|
|
38
|
|
|
$this->registerUpdateHook(TRAIT_DATEFIELDS_LAST_MODIFIED, 'DatefieldsUpdateHook'); |
|
|
|
|
|
|
39
|
|
|
$this->registerCreateHook(TRAIT_DATEFIELDS_LAST_MODIFIED, 'DatefieldsCreateHook'); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
$this->created = null; |
|
42
|
|
|
$this->lastModified = null; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
protected function DatefieldsCreateHook() |
|
46
|
|
|
{ |
|
47
|
|
|
// Should this be split up to seperate hooks for "last_modified" and "created" for consistency? |
|
48
|
|
|
$this->created = (new \DateTime('now'))->format('Y-m-d H:i:s'); |
|
49
|
|
|
$this->lastModified = (new \DateTime('now'))->format('Y-m-d H:i:s'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
protected function DatefieldsUpdateHook() |
|
53
|
|
|
{ |
|
54
|
|
|
$this->lastModified = (new \DateTime('now'))->format('Y-m-d H:i:s'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function getLastModifiedDate() |
|
58
|
|
|
{ |
|
59
|
|
|
return new \DateTime($this->lastModified); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getCreationDate() |
|
63
|
|
|
{ |
|
64
|
|
|
return new \DateTime($this->created); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.