Skip to content

Commit e3e3f6f

Browse files
committed
Merge branch 'patch-1' of github.com:KoernerWS/symfony-docs into KoernerWS-patch-1
Conflicts: cookbook/form/form_collections.rst
2 parents 8bbc918 + 958eb15 commit e3e3f6f

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

cookbook/form/form_collections.rst

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,9 @@ the relationship between the removed ``Tag`` and ``Task`` object.
665665
is handling the "update" of your Task::
666666

667667
// src/Acme/TaskBundle/Controller/TaskController.php
668-
668+
669+
use Doctrine\Common\Collections\ArrayCollection;
670+
669671
// ...
670672
public function editAction($id, Request $request)
671673
{
@@ -676,11 +678,11 @@ the relationship between the removed ``Tag`` and ``Task`` object.
676678
throw $this->createNotFoundException('No task found for is '.$id);
677679
}
678680

679-
$originalTags = array();
681+
$originalTags = new ArrayCollection();
680682

681-
// Create an array of the current Tag objects in the database
683+
// Create an ArrayCollection of the current Tag objects in the database
682684
foreach ($task->getTags() as $tag) {
683-
$originalTags[] = $tag;
685+
$originalTags->add($tag);
684686
}
685687

686688
$editForm = $this->createForm(new TaskType(), $task);
@@ -689,27 +691,20 @@ the relationship between the removed ``Tag`` and ``Task`` object.
689691

690692
if ($editForm->isValid()) {
691693

692-
// filter $originalTags to contain tags no longer present
693-
foreach ($task->getTags() as $tag) {
694-
foreach ($originalTags as $key => $toDel) {
695-
if ($toDel->getId() === $tag->getId()) {
696-
unset($originalTags[$key]);
697-
}
698-
}
699-
}
700-
701694
// remove the relationship between the tag and the Task
702695
foreach ($originalTags as $tag) {
703-
// remove the Task from the Tag
704-
$tag->getTasks()->removeElement($task);
696+
if (false === $task->getTags()->contains($tag)) {
697+
// remove the Task from the Tag
698+
$tag->getTasks()->removeElement($task);
705699

706-
// if it were a many-to-one relationship, remove the relationship like this
707-
// $tag->setTask(null);
700+
// if it was a many-to-one relationship, remove the relationship like this
701+
// $tag->setTask(null);
708702

709-
$em->persist($tag);
703+
$em->persist($tag);
710704

711-
// if you wanted to delete the Tag entirely, you can also do that
712-
// $em->remove($tag);
705+
// if you wanted to delete the Tag entirely, you can also do that
706+
// $em->remove($tag);
707+
}
713708
}
714709

715710
$em->persist($task);

0 commit comments

Comments
 (0)