简体   繁体   中英

symfony2 embedded collection edit form issue

I have a problem with edititng embedded collection form. I have two object with many-to-one relation. When I create an object "Good" with related "photos" all successfully. When I update the Good object by adding some new photos all works fine too. But, if I try to delete a one photo in some Good object after update photo is not deleted.

Good.php

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

/**
 * Add photos
 *
 * @param \VDKP\Site\BackendBundle\Entity\Photo $photos
 * @return Good
 */
public function addPhoto(\VDKP\Site\BackendBundle\Entity\Photo $photos)
{
    $photos->setGood($this);

    $this->photos->add($photos);

    return $this;
}

/**
 * Remove photos
 *
 * @param \VDKP\Site\BackendBundle\Entity\Photo $photos
 */
public function removePhoto(\VDKP\Site\BackendBundle\Entity\Photo $photos)
{
    $this->photos->removeElement($photos);
}

/**
 * Get photos
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getPhotos()
{
    return $this->photos;
}

Photo.php

/**
 * @ORM\ManyToOne(targetEntity="Good", inversedBy="photos")
 * @ORM\JoinColumn(name="good_id", referencedColumnName="id")
 **/
private $good;

GoodController, updateACtion:

public function updateAction(Request $request, $id)
{
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('VDKPSiteBackendBundle:Good')->find($id);

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

    $originalPhotos = new \Doctrine\Common\Collections\ArrayCollection();

    foreach ($entity->getPhotos() as $photo) {
        $originalPhotos->add($photo);
    }

    $editForm = $this->createEditForm($entity);

    $editForm->handleRequest($request);

    if ($editForm->isValid()) {


        foreach ($originalPhotos as $photo) {
            if (false === $entity->getPhotos()->contains($photo)) {
                $photo->setGood(null);

                $em->persist($photo);                
            }
        }

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

    return $this->redirect($this->generateUrl('good_edit', array('id' => $id)));

    return array(
        'entity'      => $entity,
        'edit_form'   => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    );

}

I did everything as written in the documentation here .

Sorry for my english. Thank you for your help.

It looks like you missed this part of docs:

foreach ($originalTags as $tag) {
    if (false === $task->getTags()->contains($tag)) {
        // remove the Task from the Tag
        $tag->getTasks()->removeElement($task);

        // if it was a many-to-one relationship, remove the relationship like this
        // $tag->setTask(null);

        $em->persist($tag);

        // if you wanted to delete the Tag entirely, you can also do that
        // $em->remove($tag);
    }
}

So, I think you have to do something similar with your data types: Good and Photo.

I think, documentation is inaccurate, because:

In this part of code:

$originalTags = new ArrayCollection();

// Create an ArrayCollection of the current Tag objects in the database
foreach ($task->getTags() as $tag) {
   $originalTags->add($tag);
}

we collect Tags, which have relations with current Task in database.

In this part of code:

foreach ($originalTags as $tag) {
    if (false === $task->getTags()->contains($tag)) {
        // remove the Task from the Tag
        $tag->getTasks()->removeElement($task);

        // if it was a many-to-one relationship, remove the relationship like this
        // $tag->setTask(null);

        $em->persist($tag);

        // if you wanted to delete the Tag entirely, you can also do that
        // $em->remove($tag);
    }
}

we must compare $request data and $originalTags array data. But, we compare $originalTags with $task->getTags(), which is essentially the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM