@@ -676,11 +676,11 @@ the relationship between the removed ``Tag`` and ``Task`` object.
676
676
throw $this->createNotFoundException('No task found for is '.$id);
677
677
}
678
678
679
- $originalTags = array ();
679
+ $originalTags = new \Doctrine\Common\Collections\ArrayCollection ();
680
680
681
- // Create an array of the current Tag objects in the database
681
+ // Create an ArrayCollection of the current Tag objects in the database
682
682
foreach ($task->getTags() as $tag) {
683
- $originalTags[] = $tag;
683
+ $originalTags->add( $tag) ;
684
684
}
685
685
686
686
$editForm = $this->createForm(new TaskType(), $task);
@@ -689,27 +689,20 @@ the relationship between the removed ``Tag`` and ``Task`` object.
689
689
690
690
if ($editForm->isValid()) {
691
691
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
-
701
692
// remove the relationship between the tag and the Task
702
693
foreach ($originalTags as $tag) {
703
- // remove the Task from the Tag
704
- $tag->getTasks()->removeElement($task);
694
+ if ($task->getTags()->contains($tag) == false) {
695
+ // remove the Task from the Tag
696
+ $tag->getTasks()->removeElement($task);
705
697
706
- // if it were a ManyToOne relationship, remove the relationship like this
707
- // $tag->setTask(null);
698
+ // if it were a ManyToOne relationship, remove the relationship like this
699
+ // $tag->setTask(null);
708
700
709
- $em->persist($tag);
701
+ $em->persist($tag);
710
702
711
- // if you wanted to delete the Tag entirely, you can also do that
712
- // $em->remove($tag);
703
+ // if you wanted to delete the Tag entirely, you can also do that
704
+ // $em->remove($tag);
705
+ }
713
706
}
714
707
715
708
$em->persist($task);
0 commit comments