1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Blog\Entity; |
6
|
|
|
|
7
|
|
|
use Cycle\Annotated\Annotation\Column; |
8
|
|
|
use Cycle\Annotated\Annotation\Entity; |
9
|
|
|
use Cycle\Annotated\Annotation\Relation\ManyToMany; |
10
|
|
|
use Cycle\Annotated\Annotation\Table; |
11
|
|
|
use Cycle\Annotated\Annotation\Table\Index; |
12
|
|
|
use Cycle\ORM\Relation\Pivoted\PivotedCollection; |
13
|
|
|
use DateTimeImmutable; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @Entity(repository="App\Blog\Tag\TagRepository") |
17
|
|
|
* @Table( |
18
|
|
|
* indexes={ |
19
|
|
|
* @Index(columns={"label"}, unique=true) |
20
|
|
|
* } |
21
|
|
|
* ) |
22
|
|
|
*/ |
23
|
|
|
class Tag |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @Column(type="primary") |
27
|
|
|
*/ |
28
|
|
|
private ?int $id = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @Column(type="string(255)") |
32
|
|
|
*/ |
33
|
|
|
private string $label; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @Column(type="datetime") |
37
|
|
|
*/ |
38
|
|
|
private DateTimeImmutable $created_at; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @ManyToMany(target="App\Blog\Entity\Post", though="PostTag", fkAction="CASCADE", indexCreate=false) |
42
|
|
|
* |
43
|
|
|
* @var PivotedCollection|Post[] |
44
|
|
|
*/ |
45
|
|
|
private $posts; |
46
|
|
|
|
47
|
|
|
public function __construct(string $label) |
48
|
|
|
{ |
49
|
|
|
$this->label = $label; |
50
|
|
|
$this->created_at = new DateTimeImmutable(); |
51
|
|
|
$this->posts = new PivotedCollection(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getId(): ?int |
55
|
|
|
{ |
56
|
|
|
return $this->id; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getLabel(): string |
60
|
|
|
{ |
61
|
|
|
return $this->label; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function setLabel(string $label): void |
65
|
|
|
{ |
66
|
|
|
$this->label = $label; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getCreatedAt(): DateTimeImmutable |
70
|
|
|
{ |
71
|
|
|
return $this->created_at; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return Post[] |
76
|
|
|
*/ |
77
|
|
|
public function getPosts(): array |
78
|
|
|
{ |
79
|
|
|
return $this->posts->toArray(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function addPost(Post $post): void |
83
|
|
|
{ |
84
|
|
|
$this->posts->add($post); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|