Galn89
Galn89

Reputation: 93

How to delete a part of data in a relation ManyToMany

I am a beginner on Symfony 4 and after having followed some tutorials, I am working on a project to improve myself!

It is an event manager with registrations of participants with a back office. As a visitor, I can register for one or more events from a select list of a bootstrap form. Once registered, the administrator can access his back office and see who has registered in this or that event by selecting a specific event and view the list.

My problem is that if the administrator decides to remove a participant from an event, the participant is deleted in the database and therefore all the events to which it has registered. While I would like it to be removed only from the event in question.

I think I need to create a query in the repository file of my event ...

Here are some parts of the code that I think is useful.

My Entity Participant.php who has a relationship ManyToMany:

/**
 * @ORM\ManyToMany(targetEntity="App\Entity\Event", inversedBy="participants")
 */
private $workshops;

Entity Event.php file :

/**
 * @ORM\ManyToMany(targetEntity="App\Entity\Participant", mappedBy="workshops")
 */
private $participants;

And in one of my controllers, here is the function that removes the participant from all events :

/**
 * @Route("/admin/delete_participant/{id}", name="delete_participant")
*/
    public function deleteParticipant(EntityManagerInterface $manager, Participant $participant) {

        $manager->remove($participant);
        $manager->flush();

        $this->addFlash('danger', "Participant deleted");

        return $this->redirectToRoute("admin_page", [
            'participant' => $participant,
        ]);
    }

I have to create a custo request but I block on it.

How do I solve this problem?

Upvotes: 3

Views: 226

Answers (2)

Galn89
Galn89

Reputation: 93

I've found a solution without using JoinTable

/**
 * @Route("admin/{id}/edit_event/", name="delete_participant")
 */
 public function deleteParticipant(int $id, Request $request): JsonResponse
    {
        $em = $this->getDoctrine()->getManager();

        $event = $em->getRepository(Event::class)->find($id);

        $participant = $em->getRepository(Participant::class)->find($request->request->get('participant_id'));

        $event->removeParticipant($participant);

        $em->persist($event);
        $em->flush();

        return new JsonResponse([
            'event' => $event,
        ]);
}

I use this function in my controller with an AJAX call in my Twig template!

Upvotes: 1

Mitesh Vasava
Mitesh Vasava

Reputation: 735

I would suggest @JoinTable if you're using MANY TO MANY Relationship.

Here i'm giving you steps which might be help you:

1) in Participant.php

/**
 * @ORM\ManyToMany(targetEntity="App\Entity\Event", inversedBy="participants")
* @ORM\JoinTable(name="participants_event",
     *         joinColumns = {@ORM\JoinColumn(name="participants_id", referencedColumnName="id", onDelete="CASCADE")},
     *         inverseJoinColumns={@ORM\JoinColumn(name="workshops_id", referencedColumnName="id")}
     *      )
 */
private $participantsEvent;

2) in Event.php

/**
 * @ORM\ManyToMany(targetEntity="App\Entity\Participant", mappedBy="participantsEvent")
 */
private $participants;

3) Generate Getter -Setter Method

php bin/console make:entity --regenerate

4) Doctrine Schema Update

php bin/console doctrine:schema:update --force

5) Remove participant

//$event  Object Of Event
//$participants Object of Participant
$event->removeParticipants($participants);

Upvotes: 2

Related Questions