임베디드 컬렉션 편집에 문제가 있습니다. 나는 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);
}
}
그래서, 나는 당신이 당신의 데이터 유형과 비슷한 뭔가를 생각 : 당신은 문서의이 부분을 놓친처럼
'removePhoto()'메소드를 사용하는 코드 스 니펫을 볼 수 없습니다. – DonCallisto
'$ photo-> setGood (null); '는 나에게 좋지 않습니다. 왜 당신은'$ good-> getPhotos() -> removeElement ($ photo);'를 사용하지 않았습니까? – Hpatoio
사진과 우수 엔티티 사이에 많은 관계가 있기 때문에 – miheyich