-
-
Couldn't load subscription status.
- 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 all commits
f50d43b 8201642 f06d300 b109436 b1ead3e caea896 24fb4fc 8ca6444 fba4bbf File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -757,3 +757,83 @@ the relationship between the removed ``Tag`` and ``Task`` object. | |
| .. _`Owning Side and Inverse Side`: http://docs.doctrine-project.org/en/latest/reference/unitofwork-associations.html | ||
| .. _`JSFiddle`: http://jsfiddle.net/847Kf/4/ | ||
| .. _`symfony-collection`: https://github.com/ninsuo/symfony-collection | ||
| | ||
| | ||
| | ||
| Optionnal helper to handle tags removals | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo in optional (only 1 n) | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a blank line after the headline | ||
| You can optionnally reduce the code in your controller and make a reusable service to handle all your embedded form. | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same typo in optionNaly and only one L | ||
| | ||
| First create a helper:: | ||
| | ||
| // src\Helper | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. src/Helper/Helper.php but we should find a better name than „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(); | ||
| | ||
| // Create an ArrayCollection of the current objects in the database | ||
| foreach ($entities as $entity) { | ||
| $original_entities->add($entity); | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use cameCase for variable names | ||
| } | ||
| | ||
OskarStark marked this conversation as resolved. Show resolved Hide resolved | ||
| return $original_entities; | ||
| } | ||
| | ||
| // 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); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| | ||
| Second edit your TaskController:: | ||
| | ||
| // src/Controller/TaskController.php | ||
| use App\Entity\Task; | ||
| | ||
| // ... | ||
| public function edit($id, Request $request) | ||
| { | ||
| $entityManager = $this->getDoctrine()->getManager(); | ||
| $task = $entityManager->getRepository(Task::class)->find($id); | ||
| | ||
| if (!$task) { | ||
| throw $this->createNotFoundException('No task found for id '.$id); | ||
| } | ||
| | ||
| $original_entities = $helper->backupOriginalEntities($event->getOrganizationTypes()); | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
| ||
| $editForm = $this->createForm(TaskType::class, $task); | ||
| $editForm->handleRequest($request); | ||
| | ||
| if ($editForm->isValid()) { | ||
| // remove the relationship between the tag and the Task. depending on your needs : add the parameter true to remove the entity or let blank if you want to keep an orphan | ||
| $helper->removeRelation($original_entities,$event,$task->getTags(),'removeTag',true); // | ||
| $entityManager->persist($task); | ||
| $entityManager->flush(); | ||
| | ||
| // redirect back to some edit page | ||
| return $this->redirectToRoute('task_edit', array('id' => $id)); | ||
| } | ||
| | ||
| // render some form template | ||
| } | ||
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.
Please remove one blank line