从oneToMany关系中删除一个项目 [英] Delete an item from oneToMany relationship

查看:88
本文介绍了从oneToMany关系中删除一个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下画廊实体

class Gallery
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var ArrayCollection
     * @ORM\OneToMany(targetEntity="Tessa\GalleryBundle\Entity\Photo", mappedBy="gallery", cascade={"persist", "remove"})
     */
    private $photos;

    /* ... */
}

gallery manyToOne PointOfInterest 实体的关系链接。这是声明

This gallery is linked with a manyToOne relationship to a PointOfInterest entity. Here is the declaration

class PointOfInterest
{
 /* ... */
 /**
 * @ORM\ManyToOne(targetEntity="Tessa\GalleryBundle\Entity\Gallery", cascade={"persist", "remove"})
 * @ORM\JoinColumn(nullable=false)
 */
private $gallery;
 /* ... */

我还使用表单更新 PointOfInterest 实体。这是表单声明

I also use a Form to update the PointOfInterest entity. Here is the form declaration

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
            ->add('name',           'text')
            ->add('gallery',        new GalleryType())
       ;
}

GalleryType 声明。

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('photos', 'collection', array('type'          => new PhotoType(),
                                            'required'      => false,
                                            'allow_add'     => true,
                                            'allow_delete'  => true,
                                            'by_reference'  => false
                                            ))
    ;
}

当我编辑 PoI 我可以添加照片到画廊没有问题,但我不能删除任何东西。

When I edit the PoI I can add photos to the gallery without problem, but I can't delete anything.

我试图挂钩画廊 PreUpdate ,但从未被调用。我在 removePhotos 方法中打印了 Gallery 实体的输出,照片从图库中删除。然后我怀疑画廊永远不会被保留下来。

I tried to hook on gallery PreUpdate, but it is never called. I printed output in removePhotos method of Gallery entity, and the photos are removed from the gallery. I then suspect the Gallery to never be persisted.

编辑后,我坚持 PoI

private function handleForm($elem, $is_new)
{
    $form = $this->createForm(new CircuitType, $elem);

    $request = $this->get('request');
    if ($request->getMethod() == 'POST') {
        $form->bind($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($elem);
            $em->flush();

            return $this->redirect($this->generateUrl('tessa_circuit_index'));
        }
    }

    return $this->render('TessaUserBundle:Circuits:add.'.'html'.'.twig',
        array(
            'form' => $form->createView(),
            'is_new' => $is_new,
        ));
}


推荐答案

关于处理这种情况的Symfony2菜谱中的内容。由于您有OneToMany关系,您必须在控制器中手动删除相关对象。

There is article in Symfony2 cookbook about handling this type of situation. As you have OneToMany relationship, you have to remove related objects manually in controller.

编辑:
或者您可以使用Doctrine的孤儿删除功能。

Or you can make use of Doctrine's orphan removal feature.

class Gallery
{
    //...    

    /**
     * @ORM\OneToMany(targetEntity="Photo", mappedBy="gallery", cascade={"persist", "remove"}, orphanRemoval=true)
     */
    private $photos;

    //...

    public function removePhotos($photo)
    {
        $this->photos->remove($photo);
        $photo->setGallery(null);
    }
}

这篇关于从oneToMany关系中删除一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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