Symfony 设置值 1 if admin 添加评论
Symfony set value 1 if admin Add the Comment
我正在使用 symfony 2.8,我在数据库的评论 table 中有状态字段,所以如果管理员从博客页面添加任何评论,它应该在数据库中保存 1 作为状态。现在我正在保存来自 showAction() 方法的评论数据。
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));
}
现在如果普通用户添加任何评论,我在 Comment 实体中定义了状态值 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() 方法,当管理员添加任何评论时,我希望评论 table 中的状态为 1。它在普通用户和管理员用户情况下都存储 0。非常感谢任何帮助。
更多代码可用于此问题。
我通过从评论实体中删除 setStatusValue()(Prepersist 方法)并从控制器为普通用户和管理员用户添加状态来解决这个问题。
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);
$comment->setstatus(0);
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);
}
$comments = $blog->getComment($comment);
return $this->render('Blog/show.html.twig', array('blog'=> $blog,'form' => $form->createView(),'comments'=>$comments));
}
我正在使用 symfony 2.8,我在数据库的评论 table 中有状态字段,所以如果管理员从博客页面添加任何评论,它应该在数据库中保存 1 作为状态。现在我正在保存来自 showAction() 方法的评论数据。
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));
}
现在如果普通用户添加任何评论,我在 Comment 实体中定义了状态值 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() 方法,当管理员添加任何评论时,我希望评论 table 中的状态为 1。它在普通用户和管理员用户情况下都存储 0。非常感谢任何帮助。
更多代码可用于此问题。
我通过从评论实体中删除 setStatusValue()(Prepersist 方法)并从控制器为普通用户和管理员用户添加状态来解决这个问题。
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);
$comment->setstatus(0);
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);
}
$comments = $blog->getComment($comment);
return $this->render('Blog/show.html.twig', array('blog'=> $blog,'form' => $form->createView(),'comments'=>$comments));
}