php - 如何在控制器的 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
是空的,我不知道为什么..谢谢大家
解决方案
推荐阅读
- c++ - 保持 boost::allocators 作为类成员。可以使用它们来初始化容器
- html - 引导模式未显示在操作中
- standardjs - 找不到模块“babel-eslint”(标准和 babel-eslint 已在本地安装)
- delphi - 如何获取 TPersistentClass 来自的动态包?
- c++ - 反转字符串?更优化的方式
- xml - 行错误:76 col:82 Line:76 ';' 预期,得到 char[61]
- push - ejabberd mod_push 和 mod_push_keepalive 不启动
- sql - 如何使用 sql server 根据上一行和下一行显示记录
- reactjs - React componentDid mount 获取异步图像。如果在更改页面之前没有完成,如何取消请求?
- codenameone - CN1:带 Alpha 通道的渐变