symfony2嵌入式集合编辑表单问题

时间:2014-10-03 06:06:14

标签: forms symfony collections editing

我对edititng嵌入式收藏表格有疑问。我有两个具有多对一关系的对象。当我创建一个对象" Good"与相关的"照片"都成功了。当我通过添加一些新照片来更新Good对象时,一切正常。但是,如果我在删除更新照片后尝试删除某个Good对象中的一张照片。

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

}

我做了文档here中所写的所有内容。

抱歉我的英文。谢谢你的帮助。

2 个答案:

答案 0 :(得分:0)

看起来你错过了这部分文档:

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

所以,我认为你必须对你的数据类型做一些类似的事情:Good和Photo。

答案 1 :(得分:0)

我认为,文档不准确,因为:

在这部分代码中:

$originalTags = new ArrayCollection();

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

我们收集与数据库中当前任务有关系的标签。

在这部分代码中:

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

我们必须比较$ request数据和$ originalTags数组数据。但是,我们将$ originalTags与$ task-> getTags()进行比较,这基本上是相同的。