Reputation: 35
I'm a beginner in symfony and I would like to inject my post entity into a method of my controller.
However, I've the following error fired :
Unable to guess how to get a Doctrine instance from the request information for parameter "post".
Here is my code :
/**
* @Route("/post/article/new", name="new.html.twig")
* @param Request $request
* @param Posts $posts
* @return Response
*/
public function newArticle(Request $request, Posts $posts): Response
{
$post = $posts;
$article = new Articles();
$post->setAuthor(1);
$form = $this->createForm(PostsType::class, $post);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
$this->em->persist($post);
$this->em->flush();
$article->setPost($post->getId());
$themes = $form->get('themes')->getData();
$article->setThemes(implode(',', $themes));
$this->em->persist($post);
$this->em->flush();
return $this->redirectToRoute('home.html.twig');
}
return $this->render('post/article.html.twig', [
'formArticle' => $form->createView()
]);
}
Upvotes: 0
Views: 834
Reputation: 10897
You need a reference to the Post in the route, like a slug for the id or another unique field.
@Route("/post/{id}/article/new", ...
Otherwise Symfony has no idea which Post to load.
Upvotes: 1