Symfony 3 orphanRemoval=true 不会从表单中删除已删除的记录



我需要创建一个总线添加表单。巴士可以有很多便利设施,这就是为什么我在巴士和巴士设施之间建立了一对一的关系。

我想要实现的是让 Doctrine 从 OneToMany 表中删除(或更新(数据,而无需在控制器中循环或执行任何特定操作。我在某处读到您可以在实体上使用 orphanRemoval=true 注释来解决这个问题,但由于某种原因它不起作用......

我可以更新现有和添加新便利设施(新数据(,但无法删除以前从编辑表单中删除的记录。

可以使用orphanRemoval=true来完成吗,或者有人知道我的代码的问题在哪里:

这是代码:

总线实体(缩短版本(:

/**
     * @ORMOneToMany(targetEntity="BusVehiclesAmenities", mappedBy="bus", cascade={"persist", "remove"}, orphanRemoval=true)
     */
    private $busVehiclesAmenities;
    public function __construct()
    {
        $this->busVehiclesAmenities = new ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }
    /**
     * Add busVehiclesAmenities
     *
     * @param AppBundleEntityBusVehiclesAmenities busVehiclesAmenities
     * @return BusVehicles
     */
    public function addBusVehiclesAmenities(BusVehiclesAmenities $busVehiclesAmenities)
    {
       /* if ($this->busVehiclesAmenities->contains($busVehiclesAmenities)) {
            return;
        }*/
        $this->busVehiclesAmenities->add($busVehiclesAmenities);
        $busVehiclesAmenities->addBus($this);
        return $this;
    }
    /**
     * Remove busVehiclesAmenities
     *
     * @param AppBundleEntityBusVehiclesAmenities $busVehiclesAmenities
     */
    public function removeBusVehiclesAmenities(BusVehiclesAmenities $busVehiclesAmenities)
    {
        $this->busVehiclesAmenities->removeElement($busVehiclesAmenities);
        $busVehiclesAmenities->addBus(null);

    }
    /**
     * Get busVehiclesAmenities
     *
     * @return ArrayCollection
     */
    public function getBusVehiclesAmenities()
    {
        return $this->busVehiclesAmenities;
    }
    /**
     * Set busVehiclesAmenities
     *
     * @return ArrayCollection
     */
    public function setBusVehiclesAmenities($busVehiclesAmenities)
    {
        $this->busVehiclesAmenities = $busVehiclesAmenities;
    }

设施实体:

private $bus;
/**
 *
 * @ORMManyToOne(targetEntity="Amenities", inversedBy="amenities")
 * @ORMJoinColumn(name="amenities_id", referencedColumnName="id")
 *
 */
private $amenities;
/**
 * @var float
 * @ORMColumn(name="price", type="float", scale=2)
 */
protected $price;
public function __construct()
{
}

/**
 * Get id
 *
 * @return integer
 */
public function getId()
{
    return $this->id;
}

/**
 * Get busId
 *
 * @return integer
 */
public function getBus()
{
    return $this->bus;
}
/**
 * Add bus
 *
 * @param AppBundleEntityBusVehicles
 *
 * @return BusVehiclesAmenities
 */
public function addBus(BusVehicles $bus)
{
    $this->bus = $bus;
    //$bus->setBusVehiclesAmenities($this);

    return $this;
} 

控制器:

 public function editAction($id, Request $request)
    {
        $busRepository = $this->getDoctrine()->getRepository('AppBundle:BusVehicles');
        $bus = $busRepository->find($id);
        $editForm = $this->createForm(BusVehiclesType::class, $bus);
        $editForm->handleRequest($request);
        if ($editForm->isSubmitted() && $editForm->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $entity = $editForm->getData();
            dump ($entity);
            $em->persist($entity); //this should take care of everything saving the manyToMany records
            $em->flush();
            // Adding flash message to our user
            $this->addFlash('success', 'New vehicle successfully added');

            //  return $this->redirectToRoute('bus_add');
        }
        return [
            'form' => $editForm->createView(),
        ];
    }

我遇到了同样的问题,但查看您的代码,我注意到您正在调用

$busVehiclesAmenities->addBus(null); 

而不是

$busVehiclesAmenities->setBus(null);

相关内容

  • 没有找到相关文章

最新更新