Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
939 views
in Technique[技术] by (71.8m points)

symfony - Delete an item from oneToMany relationship

I have the following Gallery entity

class Gallery
{
    /**
     * @var integer
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var ArrayCollection
     * @ORMOneToMany(targetEntity="TessaGalleryBundleEntityPhoto", mappedBy="gallery", cascade={"persist", "remove"})
     */
    private $photos;

    /* ... */
}

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

class PointOfInterest
{
 /* ... */
 /**
 * @ORMManyToOne(targetEntity="TessaGalleryBundleEntityGallery", cascade={"persist", "remove"})
 * @ORMJoinColumn(nullable=false)
 */
private $gallery;
 /* ... */

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())
       ;
}

and the GalleryType declaration.

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
                                            ))
    ;
}

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

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.

Here is the code when I persist the PoI after editing.

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,
        ));
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

class Gallery
{
    //...    

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

    //...

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...