-2

I have a ManyToMany relationship between two entities (ClientProject & Tour) that is actually working except from one case. When I want to clear all relations between those entities, it doesn't work and the database is not updated accordingly.

ClientProject.php

/**
 * @ORM\ManyToMany(targetEntity="Tour", inversedBy="clientProjects")
 * @ORM\JoinTable(
 *  name="client_projects_tour_items",
 *  joinColumns={
 *      @ORM\JoinColumn(name="client_project_id", referencedColumnName="id")
 *  },
 *  inverseJoinColumns={
 *      @ORM\JoinColumn(name="tour_id", referencedColumnName="id")
 *  }
 *  )
 */
 protected $tourItems;


public function removeTourItem(\SharedBundle\Entity\Tour $tourItem)
{
    $this->tourItems->removeElement($tourItem);
}

Tour.php

public function removeClientProject(\SharedBundle\Entity\ClientProject $clientProject)
{
    $this->clientProjects->removeElement($clientProject);
}

Working example:

  1. Before update:
    • Tour 1 / ClientProject 3
    • Tour 1 / ClientProject 4
  2. Action: remove relation between Tour 1 and ClientProject 4
  3. After update:
    • Tour 1 / ClientProject 3

Failing example:

  1. Before update:
    • Tour 1 / ClientProject 3
  2. Action: remove relation between Tour 1 and ClientProject 3
  3. After update:
    • Tour 1 / ClientProject 3

Controller action

public function editClientProjectAction($id, Request $request)
{
if (!$request->isXmlHttpRequest()) {
   return $this->redirectToRoute('contact');
}

$clientProject = $this->getDoctrine()->getRepository(ClientProject::class)->find($id);
if(!$clientProject) {
    return new JsonResponse(array('redirect' => $this->generateUrl('contacts')));
}

$form = $this->createForm(ClientProjectFormType::class, $clientProject, array(
    'action' => $this->generateUrl('edit-client-project', array('id' => $id))
));
$form->setData($clientProject);
if ($request->isMethod('POST')) {
    $form->submit($request->request->get($form->getName()), false);
    if($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($clientProject);
        $em->flush(); 

        return new JsonResponse(array('reloadDatatable' => 'client_project_datatable'));
    } else {
        return new JsonResponse(array('reload' => true));
    }
}

return $this->render('@AppBundle/client-projects/edit-client-project.html.twig', [
    'form' => $form->createView(),
]);
}
burki
  • 2,946
  • 6
  • 37
  • 51

1 Answers1

0

In order to make the remove to work, I think you need to add this code to your remove methods:

# Entity/ClientProject.php
/**
 * Remove tourItem
 *
 * @param \SharedBundle\Entity\Tour $tourItem
 */
public function removeTourItem(\SharedBundle\Entity\Tour $tourItem)
{
    if (!$this->tourItems->contains($tourItem)) {
        return;
    }
    $this->tourItems->removeElement($tourItem);
    $clientProject->removeTourItem($this);
}

# Entity/Tour.php
/**
 * Remove clientProject
 *
 * @param \SharedBundle\Entity\ClientProject $clientProject
 */
public function removeClientProject(\SharedBundle\Entity\ClientProject $clientProject)
{
    if (!$this->clientProject->contains($clientProject)) {
        return;
    }
    $this->clientProjects->removeElement($clientProject);
    $tourItem->removeClientProject($this);
}

Also, in the Tour.php file you need to add this annotation:

cascade={"persist", "remove"}

when you set up the relation.

Dan Costinel
  • 1,716
  • 1
  • 15
  • 23
  • @burki then it means you didn't set up correctly your relationship. [here's](https://stackoverflow.com/a/46064213/4548751) an example of a many-to-many relation. – Dan Costinel Sep 10 '17 at 19:57