2014-10-03 3 views
0

임베디드 컬렉션 편집에 문제가 있습니다. 나는 many-to-one 관계를 갖는 두 개의 객체를 가진다. 내가 "성공적으로"관련된 "사진"으로 "좋은"객체를 만들 때. 새로운 사진을 추가하여 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에 기록 된대로 내가 다했다.

죄송합니다. 도와 줘서 고마워. 좋은 및 사진 :

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

'removePhoto()'메소드를 사용하는 코드 스 니펫을 볼 수 없습니다. – DonCallisto

+0

'$ photo-> setGood (null); '는 나에게 좋지 않습니다. 왜 당신은'$ good-> getPhotos() -> removeElement ($ photo);'를 사용하지 않았습니까? – Hpatoio

+0

사진과 우수 엔티티 사이에 많은 관계가 있기 때문에 – miheyich

답변

0

는 것 같습니다. 내가 생각

+0

내 updateAction의 코드는 다음과 같습니다. foreach ($ originalPhotos as $ photo) { if (false === $ entity-> getPhotos() -> contains $ photo)) { $ photo-> setGood (null); $ em-> persist ($ photo); } } ' – miheyich

+0

아니요, 그렇지 않습니다. 당신은 setGood()하지만 아무것도 제거하지 마십시오. 다시 한 번 확인해보십시오. – Alex

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()'를 호출하면 변경된 값을 반환하지만 아직 플러시하지는 않았지만 (원래 값은 변경되지 않음) 엔티티의 테이블을 보면이 순간에 db 테이블을 보았습니다.) 이것은'data_class' 작업의 일부입니다. – xurshid29