Skip to content
80 changes: 80 additions & 0 deletions form/form_collections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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


Copy link
Contributor

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


Optionnal helper to handle tags removals
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in optional (only 1 n)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Copy link
Contributor

Choose a reason for hiding this comment

The 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.
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use cameCase for variable names

}

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());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$helper and $event is not defined

$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
}