2014-10-03 20 views
0

我对edititng嵌入式收藏表单有问题。我有两对多关系的对象。当我用相关的“照片”成功创建一个“良好”的对象时。当我通过添加一些新照片更新Good对象时,所有工作都很好。但是,如果我在删除更新照片后尝试删除某个Good对象中的一张照片。symfony2嵌入式收藏编辑表单问题

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英寸

对不起,我的英语。感谢您的帮助。

+0

我看不到使用'removePhoto()'方法的代码片段 – DonCallisto 2014-10-03 06:42:30

+0

它是'$ photo-> setGood(null);'对我来说看起来不太好。为什么不使用'$ good-> getPhotos() - > removeElement($ photo);'? – Hpatoio 2014-10-03 07:41:54

+0

因为我在照片和好实体之间有多对一的关系 – miheyich 2014-10-03 08:12:24

回答

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

所以,我认为你必须与你的数据类型类似的东西:好和照片。

+0

它的代码在我的updateAction中:foreach($ originalPhotos as $ photo)if(false === $ entity-> getPhotos() - > contains( $ photo)){ $ photo-> setGood(null); $ em-> persist($ photo); } }' – miheyich 2014-10-03 07:03:19

+0

不,不是。你只做setGood(),但不要删除任何东西。再次检查出来。 – Alex 2014-10-03 09:32:40

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

我们必须比较$请求数据和$ originalTags阵列数据。但是,我们将$ originalTags与$ task-> getTags()进行了比较,后者基本相同。

+0

如果您在表单中更改某些字段的值,将其提交并调用'$ myEntity-> getMyField()',它会返回已更改的值,但您尚未刷新它(但可以在您的视图中看到原始(未更改)的值实体的表,如果你在这个时候看数据库表),这是我认为'data_class'工作的一些。 – xurshid29 2014-10-03 09:51:16