1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cycle\ORM\Entity\Behavior; |
6
|
|
|
|
7
|
|
|
use Cycle\ORM\Entity\Behavior\Schema\BaseModifier; |
8
|
|
|
use Cycle\ORM\Entity\Behavior\Schema\RegistryModifier; |
9
|
|
|
use Cycle\ORM\Entity\Behavior\Listener\UpdatedAt as Listener; |
10
|
|
|
use Cycle\ORM\Schema\GeneratedField; |
11
|
|
|
use Cycle\Schema\Registry; |
12
|
|
|
use Doctrine\Common\Annotations\Annotation\Attribute; |
13
|
|
|
use Doctrine\Common\Annotations\Annotation\Attributes; |
14
|
|
|
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor; |
15
|
|
|
use Doctrine\Common\Annotations\Annotation\Target; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* UpdatedAt behavior will automate adding an updating date to your entity. The behavior has three parameters: |
19
|
|
|
* - field - is a property in the entity |
20
|
|
|
* - column - is a column in the database |
21
|
|
|
* - nullable - if this parameter is set to false, will be set initial value when an entity is creating |
22
|
|
|
* Behavior requires a field with the DateTime type. |
23
|
|
|
* A property in an entity and a field in the database can be added in several ways: |
24
|
|
|
* - Can be added by a behavior automatically. |
25
|
|
|
* - Can be configured with an existing field of the required type in the entity. |
26
|
|
|
* If the existing field is not of the correct type, or if the property is set for a field in the database that is |
27
|
|
|
* different from the one specified in the behavior parameters, an exception will be thrown. |
28
|
|
|
* |
29
|
|
|
* @Annotation |
30
|
|
|
* @NamedArgumentConstructor() |
31
|
|
|
* @Target({"CLASS"}) |
32
|
|
|
* @Attributes({ |
33
|
|
|
* @Attribute("field", type="string"), |
34
|
|
|
* @Attribute("column", type="string"), |
35
|
|
|
* @Attribute("nullable", type="boolean") |
36
|
|
|
* }) |
37
|
|
|
*/ |
38
|
|
|
#[\Attribute(\Attribute::TARGET_CLASS), NamedArgumentConstructor] |
39
|
|
|
final class UpdatedAt extends BaseModifier |
40
|
|
|
{ |
41
|
|
|
private ?string $column = null; |
42
|
24 |
|
|
43
|
|
|
public function __construct( |
44
|
|
|
private string $field = 'updatedAt', |
45
|
|
|
?string $column = null, |
46
|
|
|
private bool $nullable = false, |
47
|
24 |
|
) { |
48
|
|
|
$this->column = $column; |
49
|
|
|
} |
50
|
16 |
|
|
51
|
|
|
public function compute(Registry $registry): void |
52
|
16 |
|
{ |
53
|
|
|
$modifier = new RegistryModifier($registry, $this->role); |
54
|
|
|
$this->column = $modifier->findColumnName($this->field, $this->column); |
55
|
16 |
|
|
56
|
|
|
if ($this->column !== null) { |
57
|
|
|
$modifier->addDatetimeColumn($this->column, $this->field); |
58
|
16 |
|
} |
59
|
16 |
|
} |
60
|
|
|
|
61
|
|
|
public function render(Registry $registry): void |
62
|
|
|
{ |
63
|
24 |
|
$modifier = new RegistryModifier($registry, $this->role); |
64
|
|
|
|
65
|
24 |
|
$this->column = $modifier->findColumnName($this->field, $this->column) ?? $this->field; |
66
|
24 |
|
|
67
|
|
|
$modifier->addDatetimeColumn( |
68
|
24 |
|
$this->column, |
69
|
24 |
|
$this->field, |
70
|
|
|
GeneratedField::BEFORE_INSERT | GeneratedField::BEFORE_UPDATE, |
71
|
|
|
); |
72
|
|
|
} |
73
|
16 |
|
|
74
|
|
|
protected function getListenerClass(): string |
75
|
16 |
|
{ |
76
|
|
|
return Listener::class; |
77
|
16 |
|
} |
78
|
|
|
|
79
|
16 |
|
protected function getListenerArgs(): array |
80
|
|
|
{ |
81
|
|
|
return [ |
82
|
|
|
'field' => $this->field, |
83
|
|
|
'nullable' => $this->nullable, |
84
|
|
|
]; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|