1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Cycle\Mapper; |
4
|
|
|
|
5
|
|
|
use Cycle\Annotated\Annotation\Column; |
6
|
|
|
use Cycle\Annotated\Annotation\Table; |
7
|
|
|
use Cycle\ORM\Command\ContextCarrierInterface; |
8
|
|
|
use Cycle\ORM\Command\Database\Update; |
9
|
|
|
use Cycle\ORM\Heap\Node; |
10
|
|
|
use Cycle\ORM\Heap\State; |
11
|
|
|
use Cycle\ORM\Mapper\Mapper; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* You can use the annotated entities extension to automatically declare the needed columns from inside your mapper |
15
|
|
|
* @See https://github.com/cycle/docs/blob/master/advanced/timestamp.md#automatically-define-columns |
16
|
|
|
* |
17
|
|
|
* @Table( |
18
|
|
|
* columns={"created_at": @Column(type="datetime"), "updated_at": @Column(type="datetime")} |
19
|
|
|
* ) |
20
|
|
|
*/ |
21
|
|
|
class TimestampedMapper extends Mapper |
22
|
|
|
{ |
23
|
|
|
public function queueCreate($entity, Node $node, State $state): ContextCarrierInterface |
24
|
|
|
{ |
25
|
|
|
$command = parent::queueCreate($entity, $node, $state); |
26
|
|
|
|
27
|
|
|
$state->register('created_at', new \DateTimeImmutable(), true); |
28
|
|
|
$command->register('created_at', new \DateTimeImmutable(), true); |
29
|
|
|
|
30
|
|
|
$state->register('updated_at', new \DateTimeImmutable(), true); |
31
|
|
|
$command->register('updated_at', new \DateTimeImmutable(), true); |
32
|
|
|
|
33
|
|
|
return $command; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function queueUpdate($entity, Node $node, State $state): ContextCarrierInterface |
37
|
|
|
{ |
38
|
|
|
/** @var Update $command */ |
39
|
|
|
$command = parent::queueUpdate($entity, $node, $state); |
40
|
|
|
|
41
|
|
|
$state->register('updated_at', new \DateTimeImmutable(), true); |
42
|
|
|
$command->registerAppendix('updated_at', new \DateTimeImmutable()); |
43
|
|
|
|
44
|
|
|
return $command; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|