Bonjour
Aidez-moi svp !
J'ai tout à coup cette erreur que je ne comprend pas :
Voici mon entity Post.phpApp\Entity\Post object not found by the @ParamConverter annotation.
Mon controller :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268 <?php /* * This file is part of the Symfony package. * * (c) Fabien Potencier <fabien@symfony.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace App\Entity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; /** * @ORM\Entity(repositoryClass="App\Repository\PostRepository") * @ORM\Table(name="symfony_demo_post") * * Defines the properties of the Post entity to represent the blog posts. * * See https://symfony.com/doc/current/book/doctrine.html#creating-an-entity-class * * Tip: if you have an existing database, you can generate these entity class automatically. * See https://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html * * @author Ryan Weaver <weaverryan@gmail.com> * @author Javier Eguiluz <javier.eguiluz@gmail.com> * @author Yonel Ceruto <yonelceruto@gmail.com> */ class Post { /** * Use constants to define configuration options that rarely change instead * of specifying them under parameters section in config/services.yaml file. * * See https://symfony.com/doc/current/best_practices/configuration.html#constants-vs-configuration-options */ public const NUM_ITEMS = 5; /** * @var int * * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") */ private $id; /** * @var string * * @ORM\Column(type="string") * @Assert\NotBlank */ private $title; /** * @var string * * @ORM\Column(type="string") */ private $slug; /** * @var string * * @ORM\Column(type="string") * @Assert\NotBlank(message="post.blank_summary") */ private $summary; /** * @var string * * @ORM\Column(type="text") * @Assert\NotBlank(message="post.blank_content") * @Assert\Length(min=10, minMessage="post.too_short_content") */ private $content; /** * @var \DateTime * * @ORM\Column(type="datetime") * @Assert\DateTime */ private $publishedAt; /** * @var User * * @ORM\ManyToOne(targetEntity="App\Entity\User") * @ORM\JoinColumn(nullable=false) */ private $author; /** * @var Comment[]|ArrayCollection * * @ORM\OneToMany( * targetEntity="Comment", * mappedBy="post", * orphanRemoval=true, * cascade={"persist"} * ) * @ORM\OrderBy({"publishedAt": "DESC"}) */ private $comments; /** * @var Tag[]|ArrayCollection * * @ORM\ManyToMany(targetEntity="App\Entity\Tag", cascade={"persist"}) * @ORM\JoinTable(name="symfony_demo_post_tag") * @ORM\OrderBy({"name": "ASC"}) * @Assert\Count(max="4", maxMessage="post.too_many_tags") */ private $tags; public function __construct() { $this->publishedAt = new \DateTime(); $this->comments = new ArrayCollection(); $this->tags = new ArrayCollection(); //$this->image = new UploadedFile(); } public function getId(): int { return $this->id; } public function getTitle(): ?string { return $this->title; } public function setTitle(?string $title): void { $this->title = $title; } public function getSlug(): ?string { return $this->slug; } public function setSlug(?string $slug): void { $this->slug = $slug; } public function getContent(): ?string { return $this->content; } public function setContent(?string $content): void { $this->content = $content; } public function getPublishedAt(): \DateTime { return $this->publishedAt; } public function setPublishedAt(?\DateTime $publishedAt): void { $this->publishedAt = $publishedAt; } public function getAuthor(): User { return $this->author; } public function setAuthor(?User $author): void { $this->author = $author; } public function getComments(): Collection { return $this->comments; } public function addComment(?Comment $comment): void { $comment->setPost($this); if (!$this->comments->contains($comment)) { $this->comments->add($comment); } } public function removeComment(Comment $comment): void { $comment->setPost(null); $this->comments->removeElement($comment); } public function getSummary(): ?string { return $this->summary; } public function setSummary(?string $summary): void { $this->summary = $summary; } public function addTag(?Tag ...$tags): void { foreach ($tags as $tag) { if (!$this->tags->contains($tag)) { $this->tags->add($tag); } } } public function removeTag(Tag $tag): void { $this->tags->removeElement($tag); } public function getTags(): Collection { return $this->tags; } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128 <?php namespace App\Controller; use App\Entity\Comment; use App\Entity\Post; use App\Entity\User; use App\Events; use App\Form\CommentType; use App\Repository\PostRepository; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\GenericEvent; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use Symfony\Bundle\FrameworkBundle\Controller\Controller; /** * @Route("/post") */ class BlogPostController extends Controller { /** * @Route("/", defaults={"page": "1"}, name="post_index", methods="GET") * @Route("/page/{page}", requirements={"page": "[1-9]\d*"}, name="blog_index_paginated") */ public function index(int $page, PostRepository $posts): Response { $latestPosts = $posts->findLatest($page); return $this->render('post/index.html.twig', ['posts' => $latestPosts]); } /** * @Route("/posts/{slug}", name="post_show", methods="GET") */ public function show(Post $post): Response { return $this->render('post/show.html.twig', ['post' => $post]); } /** * @Route("/comment/{postSlug}/new", name="comment_new") * @Method("POST") * @Security("is_granted('IS_AUTHENTICATED_FULLY')") * @ParamConverter("post", options={"mapping": {"postSlug": "slug"}}) * * NOTE: The ParamConverter mapping is required because the route parameter * (postSlug) doesn't match any of the Doctrine entity properties (slug). * See https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html#doctrine-converter */ public function commentNew(Request $request, Post $post, EventDispatcherInterface $eventDispatcher): Response { $comment = new Comment(); $comment->setAuthor($this->getUser()); $post->addComment($comment); $form = $this->createForm(CommentType::class, $comment); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $em = $this->getDoctrine()->getManager(); $em->persist($comment); $em->flush(); // When triggering an event, you can optionally pass some information. // For simple applications, use the GenericEvent object provided by Symfony // to pass some PHP variables. For more complex applications, define your // own event object classes. // See https://symfony.com/doc/current/components/event_dispatcher/generic_event.html $event = new GenericEvent($comment); // When an event is dispatched, Symfony notifies it to all the listeners // and subscribers registered to it. Listeners can modify the information // passed in the event and they can even modify the execution flow, so // there's no guarantee that the rest of this controller will be executed. // See https://symfony.com/doc/current/components/event_dispatcher.html $eventDispatcher->dispatch(Events::COMMENT_CREATED, $event); return $this->redirectToRoute('blog_post', ['slug' => $post->getSlug()]); } return $this->render('post/comment_form_error.html.twig', [ 'post' => $post, 'form' => $form->createView(), ]); } /** * This controller is called directly via the render() function in the * blog/post_show.html.twig template. That's why it's not needed to define * a route name for it. * * The "id" of the Post is passed in and then turned into a Post object * automatically by the ParamConverter. */ public function commentForm(Post $post): Response { $form = $this->createForm(CommentType::class); return $this->render('post/_comment_form.html.twig', [ 'post' => $post, 'form' => $form->createView(), ]); } }
Partager