首页 > 解决方案 > 如何在控制器的 Symfony 中添加注释?

问题描述

我想用 symfony 在我的文章中添加评论。但我不明白为什么我有这个错误。

Argument 1 passed to Symfony\Component\Form\FormRenderer::renderBlock() must be an instance of Symfony\Component\Form\FormView, string given

我在控制器中的代码:

 /**
     * @Route("blog/comment/{id}", name="article_comment")
     */
    public function comment(Article $article,Request $request, ObjectManager $manager)
    {

        $comment = new Comment();
        $form = $this->createForm(CommentType::class, $comment);

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
           $comment->setArticle($article->getId());
           $comment->setCreatedAt(new \DateTime());

           $manager->persist($comment);
           $manager->flush();

        return $this->redirectToRoute('blog_show', ['id' => $article->getId()]);
        }

        return $this->render('blog/createComment.html.twig', [
            'formComment' => $form->createView(),
            'article' => $article
        ]);
    }

在树枝中,我使用它来调用 url:

<a href="{{path ('article_comment', {'id' : article.id })}}">Add new comment</a>

所以我需要保留文章的 ID 以告知实体评论以将其添加到“setArticle”中显示一篇文章的代码:

   /**
     * @Route("/blog/{id}", name="blog_show")
     */
    public function show(Article $article)
    {
        return $this->render('blog/show.html.twig',[
            'article' => $article
        ]);
    }

树枝形式:

{% extends 'base.html.twig' %}
{% form_theme formComment 'bootstrap_4_layout.html.twig' %}
{% block body %}

{{ form('formComment')}}


{% endblock %}

和关系:在实体评论中:

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\Article", inversedBy="comments")
 * @ORM\JoinColumn(nullable=false)
 */
private $article;

我认为参数article是空的,我不知道为什么..谢谢大家

标签: phpsymfonyrendersymfony4

解决方案


推荐阅读