-
- Notifications
You must be signed in to change notification settings - Fork 5.3k
Create helper to manage relationship removal #9709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
f50d43b 8201642 f06d300 b109436 b1ead3e caea896 24fb4fc 8ca6444 fba4bbf File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -752,9 +752,15 @@ the relationship between the removed ``Tag`` and ``Task`` object. | |
| | ||
| // App\Helper; | ||
| use Doctrine\Common\Collections\ArrayCollection; | ||
| | ||
| use Doctrine\ORM\EntityManagerInterface; | ||
| | ||
| class Helper | ||
| private $em; | ||
| ||
| | ||
| public function __construct(EntityManagerInterface $em) | ||
| { | ||
| $this->em = $em; | ||
| } | ||
| public function backupOriginalEntities($entities) | ||
| ||
| { | ||
| $original_entities = new ArrayCollection(); | ||
| | @@ -767,22 +773,16 @@ the relationship between the removed ``Tag`` and ``Task`` object. | |
| return $original_entities; | ||
| } | ||
| | ||
| //this function removes totally the entity | ||
| public function removeEntity($original_entities, $current_entities) | ||
| { | ||
| foreach ($original_entities as $entity) { | ||
| if (false === $current_entities->contains($entity)) { | ||
| $em->remove($entity); | ||
| } | ||
| } | ||
| } | ||
| | ||
| //this function removes only the relationship | ||
| public function removeRelation($original_entities, $main_entity,$current_entities, $function_name) | ||
| //this function removes only the relationship, removes the entity if $remove set to true | ||
| ||
| public function removeRelation($original_entities, $main_entity,$current_entities, $function_name, $remove=false) | ||
| { | ||
| foreach ($original_entities as $entity) { | ||
| if (false === $current_entities->contains($entity)) { | ||
| $main_entity->$function_name($entity); | ||
| | ||
| if($remove){ | ||
| $this->em->remove($entity); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| | @@ -813,12 +813,9 @@ the relationship between the removed ``Tag`` and ``Task`` object. | |
| if ($editForm->isValid()) { | ||
| | ||
| // remove the relationship between the tag and the Task | ||
| ||
| //depending on your needs | ||
| //either | ||
| $helper->removeRelation($original_entities,$event,$task->getTags(),'removeTag'); | ||
| //or | ||
| $helper->removeEntity($original_entities,$event,$task->getTags()); | ||
| | ||
| //depending on your needs | ||
| $helper->removeRelation($original_entities,$event,$task->getTags(),'removeTag',true); // or let blank if you want to keep orphan | ||
| ||
| | ||
| $entityManager->persist($task); | ||
| $entityManager->flush(); | ||
| | ||
| | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could remove the
;.