【发布时间】:2020-06-01 08:50:34
【问题描述】:
你好我试图在博客中添加评论一切都很好它没有显示错误但是当我点击提交时没有任何反应我的意思是它没有将它添加到数据库中我不知道我缺少什么 这就是我在控制器中所拥有的
public function addCommentAction(Request $request, $id)
{
$user=$this->getUser();
if($user==null)
return $this->redirectToRoute('fos_user_security_login');
$add_comment = new CommentaireBlog();
$em = $this->getDoctrine()->getManager();
$blog = $em->getRepository(Blog::class)->find($id);
$add_comment->setBlog($blog);
$add_comment->setUser($user);
$add_comment->setDate( new \DateTime());
$form = $this->createFormBuilder($add_comment)
->add('contenu', TextareaType::class)
->getForm();
if ($request->getMethod() == 'POST') {
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$add_comment = $form->getData();
$em = $this->getDoctrine()->getEntityManager();
$em->persist($add_comment);
$em->flush();
return $this->redirect($this->generateUrl('blog_details'));
}
}
return $this->render('blog/details.html.twig', array(
'form' => $form->createView(),
'comment' => $add_comment,
'blog' => $blog,
));
}
这就是我在 blog.yml 中的内容
comment_new:
path: /{id}/details
defaults: { _controller: "BlogBundle:Blog:addComment" }
methods: [GET, POST]
最后这是树枝页面
<div class="comments-form">
<h4 class="comments-title">Leave A Reply</h4>
<!-- .row -->
<form action="{{ path('comment_new', { 'id': blog.id }) }}" method="post" >
<textarea id="form_comment" name="form[comment]" required="required" class="form-control comments-textarea" placeholder="Comments*"></textarea>
<input type="submit" class="btn btn-default" />
</form>
</div>
</div>
【问题讨论】:
-
博客和cmets有关系吗?如果是这样,沿着这条线的某个地方你将需要一个 $blog->addComment($comment) 线。