【发布时间】:2017-05-16 07:53:59
【问题描述】:
我有一个主题论坛,每个主题都可以被其他用户评论。我创建的关系是:
class Comment
{
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Topic", inversedBy="comments")
*/
private $topic;
}
还有班级主题:
class Topic
{
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Comment", mappedBy="topic")
* @ORM\JoinColumn(name="comment_id", referencedColumnName="id")
*/
private $comments;
}
问题是,当我创建评论时,cmets 表中的 topic_id 列为空。我不确定如何获取当前主题 ID 并将其设置为评论。这是我在 CommentController 中的 addCommentAction() 函数:
/**
* @Route("/comment/add", name="comment_add")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function addCommentAction(Request $request)
{
$comment = new Comment();
$form = $this->createForm(CommentType::class, $comment);
$form->handleRequest($request);
if ($form->isValid()) {
$comment->setDateCreated(new \DateTime());
$em = $this->getDoctrine()->getManager();
$em->persist($comment);
$em->flush();
$this->addFlash(
'notice',
'Comment Added Successfully !'
);
}
return $this->render('comments/comment.add.html.twig', array(
'commentsForm' => $form->createView()
));
}
树枝模板中的路径:
<a href="{{ path('comment_add') }}" class="btn btn-lg btn-default">Leave a Comment</a>
【问题讨论】:
-
我认为您应该通过评论表单中的隐藏输入或通过 url GET 参数(表单的操作属性)传递
topic_id。如果您在处理表单的同一操作中呈现表单,则无论如何您都需要在 url 中使用主题 ID。