【问题标题】:Symfony Doctrine: Remove all ManyToMany relationsSymfony 原则:删除所有多对多关系
【发布时间】:2017-09-10 18:31:42
【问题描述】:

我在两个实体(ClientProject 和 Tour)之间存在多对多关系,除了一种情况外,它实际上是有效的。当我想清除这些实体之间的所有关系时,它不起作用并且数据库没有相应地更新。

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);
}

工作示例:

  1. 更新前:
    • Tour 1 / ClientProject 3
    • Tour 1 / ClientProject 4
  2. 操作:删除 Tour 1 和 ClientProject 4 之间的关系
  3. 更新后:
    • Tour 1 / ClientProject 3

失败的例子:

  1. 更新前:
    • Tour 1 / ClientProject 3
  2. 操作:删除 Tour 1 和 ClientProject 3 之间的关系
  3. 更新后:
    • Tour 1 / ClientProject 3

控制器动作

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(),
]);
}

【问题讨论】:

  • 可能会分享更多关于失败示例的代码可能有助于解决您的问题。
  • @PeterTanath 请查看更新后的问题

标签: php symfony doctrine


【解决方案1】:

为了使删除工作,我认为您需要将此代码添加到您的删除方法中:

# 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);
}

另外,在Tour.php 文件中你需要添加这个注解:

cascade={"persist", "remove"}

当你建立关系时。

【讨论】:

  • @burki 那么这意味着你没有正确建立你的关系。 here's 一个多对多关系的例子。
猜你喜欢
  • 1970-01-01
  • 2022-01-22
  • 2016-04-12
  • 2015-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-29
相关资源
最近更新 更多