0
我使用symfony 2.8,我在数据库的注释表中有状态字段,所以如果管理员从博客页面添加任何评论,它应该被保存为1作为db状态。现在我正在保存来自showAction()方法的评论数据。Symfony设置值1如果管理员添加评论
的showAction()方法
public function showAction(Request $request,$id)
{
$blog = $this->getDoctrine()->getRepository(Blog::class)->findOneById($id);
$comment = new Comment();
$form = $this->createForm(CommentType::class, $comment);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user = new User();
$comment->setUser($this->getUser());
$comment->setBlog($blog);
if ($this->get('security.context')->isGranted('ROLE_ADMIN')) {
$comment->setstatus(1);
}
$em = $this->getDoctrine()->getManager();
$em->merge($comment);
$em->flush();
return $this->redirect($this->generateUrl('blog_show', array('id' => $id)), 301);
//return $this->redirectToRoute('blog_list');
}
$comments = $blog->getComment($comment);
return $this->render('Blog/show.html.twig', array('blog'=> $blog,'form' => $form->createView(),'comments'=>$comments));
}
现在,如果普通用户添加任何评论,我已经定义的在评论实体状态值0,以便它自动插入0(PrePersist)。
评论实体
/**
* @ORM\PrePersist
*/
public function setStatusValue() {
$this->status = 0;
}
/**
* Set status
*
* @param int $status
* @return status
*/
public function setstatus($status)
{
$this->status = $status;
return $this;
}
/**
* Get status
*
* @return status
*/
public function getStatus()
{
return $this->status;
}
现在,我要的是按我的showAction()方法,我想在评语表1的状态时,管理员添加任何评论。它在正常和管理用户情况下都存储0。任何帮助深表感谢。
更多代码可在这个问题.. Symfony Save comments on blog with user and blogId
如果你对新增评论路线来管理的防火墙下,那么你可以检查'isGranted'部分否则将始终返回false和奶嘴它作为一个普通用户 –
这是检查是否符合要求。我已经检查过了。 – Aamir
请参阅dump($ comment)输出,如果条件在这里,我已经在下面添加了isGranted。 http://mysticpaste.com/9STKB8DzpY – Aamir