dqqlziv195281 2019-04-29 22:30
浏览 181
已采纳

如何删除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?

  • 写回答

2条回答 默认 最新

  • dsa45664 2019-05-09 17:38
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#单片机#的问题:Lora通讯模块hc-14电路图求内部原理图
  • ¥50 esp32 wroom 32e 芯片解锁
  • ¥15 bywave配置文件写入失败
  • ¥20 基于Simulink的ZPW2000轨道电路仿真
  • ¥15 pycharm找不到在环境装好的opencv-python
  • ¥15 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题
  • ¥15 keepalived的虚拟VIP地址 ping -s 发包测试,只能通过1472字节以下的数据包(相关搜索:静态路由)
  • ¥15 Mabatis查询数据