完整性约束违规1062重复密钥,symfony2

时间:2015-11-10 11:36:15

标签: symfony orm doctrine

我正在创建一个博客。我有两个实体,博客和图片,与OnetoOne双向关联。我在edit_image函数中收到错误。

    public function edit_imageAction($blog_id)
    {
    $em = $this->getDoctrine()->getManager();
    $blog = $em->getRepository('BloggerBlogBundle:Blog')->find($blog_id);       
    $picture_id = $blog->getImage()->getId();       


    $pictureOld = $em->getRepository('BloggerBlogBundle:Picture')->findOneById($picture_id);
    $em->remove($pictureOld);
    $em->flush();              

    $picture  = new Picture();
    $picture->setBlog($blog);
    $request = $this->getRequest();
    $picture_form = $this->createForm(new PictureType(), $picture);
    $picture_form->bind($request);

    if ($picture_form->isValid()) {
        $em->persist($picture);
        $em->flush();

        $this->get('session')->getFlashBag()->add('picture-notice', 'Your picture was successfully updated. Thank you!');

        return $this->redirect($this->generateUrl('BloggerBlogBundle_blog_show', array(
             'id'    => $blog->getId(),
             'slug'  => $blog->getSlug()))
        );
    }

    return $this->render('BloggerBlogBundle:Upload:show.html.twig', array(
        'blog_id'=> $blog_id,
        'picture_form' => $picture_form->createView()
    ));   
}

希望有人可以在这里帮助我,已经提前多了。 我的实体是:

    //Entity Picture
    class Picture
    {
        /**
         * @ORM\OneToOne(targetEntity="Blog", inversedBy="image")
         * @ORM\JoinColumn(name="blog_id", referencedColumnName="id")
         **/
        protected $blog;
    .
    .
    .
    }

    //Entity Blog
    class Blog
    {
        /**
         * @ORM\OneToOne(targetEntity="Picture", mappedBy="blog", cascade={"persist", "remove"})
         **/
         protected $image;
.
.
.
}

0 个答案:

没有答案