Symfony 使用用户和 blogId 在博客上保存评论 [英] Symfony Save comments on blog with user and blogId

查看:36
本文介绍了Symfony 使用用户和 blogId 在博客上保存评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 symfony 2.8,我在用户和博客之间创建了一对多关系(一个用户多个博客).我还创建了博客和评论之间的一对多关系.同样,用户和评论之间也是一对多的关系.现在我的评论表看起来像这样

评论表

id 主整数(11)user_id 主 int(11)blog_id 主整数(11)评论 varchar(2000)created_at 日期时间

评论实体

评论实体

博客实体

博客实体

用户实体

用户实体

博客控制器 -> ShowAction 方法

/*** @Route("/blog/show/{id}", name="blog_show")*/公共函数 showAction(Request $request,$id){$blog = $this->getDoctrine()->getRepository(Blog::class)->findOneById($id);$comment = new Comment();$form = $this->createForm(CommentType::class, $comment);$form->handleRequest($request);如果 ($form->isSubmitted() && $form->isValid()) {$user = 新用户();$blog = new Blog();$comment->setUser($this->getUser());$comment->setBlog($blog);$em = $this->getDoctrine()->getManager();$em->persist($comment);$em->flush();返回 $this->redirect($this->generateUrl('blog_show', array('id' => $id)), 301);}return $this->render('Blog/show.html.twig', array('blog'=> $blog,'form' => $form->createView()));}

现在当我从博客展示页面提交评论表单时,它显示了这个错误

通过关系 'AppBundle\Entity\Comment#blog' 发现了一个新实体,该实体未配置为对实体进行级联持久化操作:AppBundle\Entity\Blog@0000000017546b06000000001fededdf.要解决此问题:要么明确在这个未知实体上调用 EntityManager#persist() 或配置级联在映射中持久化这个关联,例如@ManyToOne(..,cascade={"persist"}).如果你找不到导致问题的实体,请执行 'AppBundle\Entity\Blog#__toString()' 来获取线索."

我在这里错过了什么.任何帮助深表感谢.谢谢

解决方案

我在您的代码中没有看到名为 $blogId 的变量,但假设它已设置,您可以尝试:

$blog = $this->getDoctrine()->getRepository(Blog::class)->findOneById($id);$comment->setBlog($blog);//将该实体传递给CommentEntity

I'm using symfony 2.8, I have created one-to-many relationship between user and blog,(one user many blogs). Also I have created one-to-many relationship between blog and comments. Similarly one-to-many relationship between user and comments as well. Now my comment table looks like this

comment table

id  Primary     int(11) 
user_id  Primary    int(11)
blog_id  Primary    int(11) 
comment             varchar(2000)  
created_at          datetime

Comment Entity

Comment Entity

Blog Entity

Blog Entity

User Entity

User Entity

Blog controller -> ShowAction method

/**
 * @Route("/blog/show/{id}", name="blog_show")
 */
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();
        $blog = new Blog();
        $comment->setUser($this->getUser());
        $comment->setBlog($blog);
        $em = $this->getDoctrine()->getManager();
        $em->persist($comment);
        $em->flush();
        return $this->redirect($this->generateUrl('blog_show', array('id' => $id)), 301);
    }

    return $this->render('Blog/show.html.twig', array('blog'=> $blog,'form' => $form->createView()));

}

Now when I submit the comment form from blog show page, it is showing me this error

"A new entity was found through the relationship 'AppBundle\Entity\Comment#blog' that was not configured to cascade persist operations for entity: AppBundle\Entity\Blog@0000000017546b06000000001fededdf. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement 'AppBundle\Entity\Blog#__toString()' to get a clue."

What I have missed here. Any help is much appreciated. Thanks

解决方案

I don't see a variable called $blogId in your code, but assuming it is set, you can try:

[Edit: Follow your example to get $blog]

$blog = $this->getDoctrine()->getRepository(Blog::class)->findOneById($id);

$comment->setBlog($blog);//Pass that entity to the CommentEntity

这篇关于Symfony 使用用户和 blogId 在博客上保存评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆