|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Symfony package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Fabien Potencier <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace App\Entity; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
15
|
|
|
use Doctrine\Common\Collections\Collection; |
|
16
|
|
|
use Doctrine\ORM\Mapping as ORM; |
|
17
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @ORM\Entity(repositoryClass="App\Repository\PostRepository") |
|
21
|
|
|
* @ORM\Table(name="symfony_demo_post") |
|
22
|
|
|
* |
|
23
|
|
|
* Defines the properties of the Post entity to represent the blog posts. |
|
24
|
|
|
* |
|
25
|
|
|
* See https://symfony.com/doc/current/book/doctrine.html#creating-an-entity-class |
|
26
|
|
|
* |
|
27
|
|
|
* Tip: if you have an existing database, you can generate these entity class automatically. |
|
28
|
|
|
* See https://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html |
|
29
|
|
|
* |
|
30
|
|
|
* @author Ryan Weaver <[email protected]> |
|
31
|
|
|
* @author Javier Eguiluz <[email protected]> |
|
32
|
|
|
* @author Yonel Ceruto <[email protected]> |
|
33
|
|
|
*/ |
|
34
|
|
|
class Post |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* Use constants to define configuration options that rarely change instead |
|
38
|
|
|
* of specifying them in app/config/config.yml. |
|
39
|
|
|
* |
|
40
|
|
|
* See https://symfony.com/doc/current/best_practices/configuration.html#constants-vs-configuration-options |
|
41
|
|
|
*/ |
|
42
|
|
|
const NUM_ITEMS = 10; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var int |
|
46
|
|
|
* |
|
47
|
|
|
* @ORM\Id |
|
48
|
|
|
* @ORM\GeneratedValue |
|
49
|
|
|
* @ORM\Column(type="integer") |
|
50
|
|
|
*/ |
|
51
|
|
|
private $id; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var string |
|
55
|
|
|
* |
|
56
|
|
|
* @ORM\Column(type="string") |
|
57
|
|
|
* @Assert\NotBlank |
|
58
|
|
|
*/ |
|
59
|
|
|
private $title; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var string |
|
63
|
|
|
* |
|
64
|
|
|
* @ORM\Column(type="string") |
|
65
|
|
|
*/ |
|
66
|
|
|
private $slug; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @var string |
|
70
|
|
|
* |
|
71
|
|
|
* @ORM\Column(type="string") |
|
72
|
|
|
* @Assert\NotBlank(message="post.blank_summary") |
|
73
|
|
|
*/ |
|
74
|
|
|
private $summary; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @var string |
|
78
|
|
|
* |
|
79
|
|
|
* @ORM\Column(type="text") |
|
80
|
|
|
* @Assert\NotBlank(message="post.blank_content") |
|
81
|
|
|
* @Assert\Length(min=10, minMessage="post.too_short_content") |
|
82
|
|
|
*/ |
|
83
|
|
|
private $content; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @var \DateTime |
|
87
|
|
|
* |
|
88
|
|
|
* @ORM\Column(type="datetime") |
|
89
|
|
|
* @Assert\DateTime |
|
90
|
|
|
*/ |
|
91
|
|
|
private $publishedAt; |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @var User |
|
95
|
|
|
* |
|
96
|
|
|
* @ORM\ManyToOne(targetEntity="App\Entity\User") |
|
97
|
|
|
* @ORM\JoinColumn(nullable=false) |
|
98
|
|
|
*/ |
|
99
|
|
|
private $author; |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @var Comment[]|ArrayCollection |
|
103
|
|
|
* |
|
104
|
|
|
* @ORM\OneToMany( |
|
105
|
|
|
* targetEntity="Comment", |
|
106
|
|
|
* mappedBy="post", |
|
107
|
|
|
* orphanRemoval=true |
|
108
|
|
|
* ) |
|
109
|
|
|
* @ORM\OrderBy({"publishedAt": "DESC"}) |
|
110
|
|
|
*/ |
|
111
|
|
|
private $comments; |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @var Tag[]|ArrayCollection |
|
115
|
|
|
* |
|
116
|
|
|
* @ORM\ManyToMany(targetEntity="App\Entity\Tag", cascade={"persist"}) |
|
117
|
|
|
* @ORM\JoinTable(name="symfony_demo_post_tag") |
|
118
|
|
|
* @ORM\OrderBy({"name": "ASC"}) |
|
119
|
|
|
* @Assert\Count(max="4", maxMessage="post.too_many_tags") |
|
120
|
|
|
*/ |
|
121
|
|
|
private $tags; |
|
122
|
|
|
|
|
123
|
1 |
|
public function __construct() |
|
124
|
|
|
{ |
|
125
|
1 |
|
$this->publishedAt = new \DateTime(); |
|
126
|
1 |
|
$this->comments = new ArrayCollection(); |
|
127
|
1 |
|
$this->tags = new ArrayCollection(); |
|
128
|
1 |
|
} |
|
129
|
|
|
|
|
130
|
5 |
|
public function getId(): int |
|
131
|
|
|
{ |
|
132
|
5 |
|
return $this->id; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
10 |
|
public function getTitle(): ?string |
|
136
|
|
|
{ |
|
137
|
10 |
|
return $this->title; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
2 |
|
public function setTitle(string $title): void |
|
141
|
|
|
{ |
|
142
|
2 |
|
$this->title = $title; |
|
143
|
2 |
|
} |
|
144
|
|
|
|
|
145
|
5 |
|
public function getSlug(): ?string |
|
146
|
|
|
{ |
|
147
|
5 |
|
return $this->slug; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
2 |
|
public function setSlug(string $slug): void |
|
151
|
|
|
{ |
|
152
|
2 |
|
$this->slug = $slug; |
|
153
|
2 |
|
} |
|
154
|
|
|
|
|
155
|
6 |
|
public function getContent(): ?string |
|
156
|
|
|
{ |
|
157
|
6 |
|
return $this->content; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
1 |
|
public function setContent(string $content): void |
|
161
|
|
|
{ |
|
162
|
1 |
|
$this->content = $content; |
|
163
|
1 |
|
} |
|
164
|
|
|
|
|
165
|
10 |
|
public function getPublishedAt(): \DateTime |
|
166
|
|
|
{ |
|
167
|
10 |
|
return $this->publishedAt; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
1 |
|
public function setPublishedAt(\DateTime $publishedAt): void |
|
171
|
|
|
{ |
|
172
|
1 |
|
$this->publishedAt = $publishedAt; |
|
173
|
1 |
|
} |
|
174
|
|
|
|
|
175
|
8 |
|
public function getAuthor(): User |
|
176
|
|
|
{ |
|
177
|
8 |
|
return $this->author; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
1 |
|
public function setAuthor(User $author): void |
|
181
|
|
|
{ |
|
182
|
1 |
|
$this->author = $author; |
|
183
|
1 |
|
} |
|
184
|
|
|
|
|
185
|
2 |
|
public function getComments(): Collection |
|
186
|
|
|
{ |
|
187
|
2 |
|
return $this->comments; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
1 |
|
public function addComment(Comment $comment): void |
|
191
|
|
|
{ |
|
192
|
1 |
|
$comment->setPost($this); |
|
193
|
1 |
|
if (!$this->comments->contains($comment)) { |
|
194
|
1 |
|
$this->comments->add($comment); |
|
195
|
|
|
} |
|
196
|
1 |
|
} |
|
197
|
|
|
|
|
198
|
|
|
public function removeComment(Comment $comment): void |
|
199
|
|
|
{ |
|
200
|
|
|
$comment->setPost(null); |
|
|
|
|
|
|
201
|
|
|
$this->comments->removeElement($comment); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
8 |
|
public function getSummary(): ?string |
|
205
|
|
|
{ |
|
206
|
8 |
|
return $this->summary; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
1 |
|
public function setSummary(string $summary): void |
|
210
|
|
|
{ |
|
211
|
1 |
|
$this->summary = $summary; |
|
212
|
1 |
|
} |
|
213
|
|
|
|
|
214
|
|
|
public function addTag(Tag $tag): void |
|
215
|
|
|
{ |
|
216
|
|
|
if (!$this->tags->contains($tag)) { |
|
217
|
|
|
$this->tags->add($tag); |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
public function removeTag(Tag $tag): void |
|
222
|
|
|
{ |
|
223
|
|
|
$this->tags->removeElement($tag); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
9 |
|
public function getTags(): Collection |
|
227
|
|
|
{ |
|
228
|
9 |
|
return $this->tags; |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: