symfony manytomany添加并且不删除旧图片

时间:2016-01-18 17:58:34

标签: symfony add

我目前拥有一个ManyToMany关系,我想在privtephoto实体中添加一个图像,而不删除现有的图像,因为现在当我运行我的表单时,它会删除旧的我并添加新闻

所以我想在私人照片中添加新图片而不删除现有实体

这是我的代码:

/**
 * @ORM\ManyToMany(targetEntity="Acme\AppBundle\Entity\Image", cascade={"persist", "remove"})
 * @ORM\JoinTable(name="acme_gallery_images",
 *      joinColumns={@ORM\JoinColumn(name="gallery_id", referencedColumnName="id", onDelete="CASCADE"))},
 *      inverseJoinColumns={@ORM\JoinColumn(name="image_id", referencedColumnName="id", unique=true, onDelete="CASCADE"))}
 *      )
 * @Assert\Valid()
 **/
protected $images;




public function updatePhotoAction(Request $request, $idGallery)
{
    $em     = $this->getDoctrine()->getManager();
    $entity = $em->getRepository('AcmePrivatephotoBundle:Privatephoto')->find($idGallery);

    if(!$entity)
        throw $this->createNotFoundException('Unable to find Privatephoto entity.');

    $editForm = $this->createEditPhotoForm($entity, $idGallery);
    $editForm->handleRequest($request);

    if($editForm->isValid())
    {
        $entity->setUser($this->getUser());

        $images = $entity->getImages();

        foreach($images as $img)
        {
            $img->setUser($this->getUser());
            $img->setDir('/gallery/'.$entity->getId());
            $em->persist($img);
        }

        $em->persist($entity);
        $em->flush();

        return $this->redirect($this->generateUrl('gallery_show', array('id' => $idGallery)));
    }

    return $this->render('AcmePrivatephotoBundle:Privatephoto:createPhoto.html.twig', array(
        'entity'      => $entity,
        'edit_photo_form'   => $editForm->createView(),
    ));
}

谢谢!

0 个答案:

没有答案