Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
935 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...