[英]symfony2 embedded collection edit form issue
I have a problem with edititng embedded collection form. 我对edititng嵌入式收藏表格有疑问。 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 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 Photo.php
/**
* @ORM\ManyToOne(targetEntity="Good", inversedBy="photos")
* @ORM\JoinColumn(name="good_id", referencedColumnName="id")
**/
private $good;
GoodController, updateACtion: 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. 因此,我认为您必须对数据类型执行类似的操作:Good和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. 我们收集标签,这些标签与数据库中的当前Task有关系。
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. 我们必须比较$ request数据和$ originalTags数组数据。 But, we compare $originalTags with $task->getTags(), which is essentially the same.
但是,我们将$ originalTags与$ task-> getTags()进行比较,这基本上是相同的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.