doulu7921 2014-09-24 17:57
浏览 48

在Symfony中,如何在编辑表单中删除ManyToMany相关的实体关系

I have a create form for an entity which works the way it is supposed to. The entity for this form is related ManyToMany with another entity, and that other entity is used to populate a multiple select field.

When I save the create form and select multiple options from that multiple select field, the associations between the form type's entity and the related entity are saved correctly.

However, I also have an edit form which uses the same form type class and template. When I load the edit form and the information for the existing entity is filled into it automatically, I see that the relationships with the multiple select field entity are indeed reflected; the appropriate fields of the multiple select box are selected.

When I go to save the edit form is when the problem arises. If I DEselect options in the multiple select field, I would expect that upon saving the edit form that those options would become disassociated, but this is not the case. Instead, the original relationships are preserved as if no options were changed.

What else do I need to do so that when I DEselect options on the multiple select, those options will become disassociated? Thank you!

In the Entity:

/**
 * unidirectional ManyToMany
 * @ORM\ManyToMany(targetEntity="\Myco\ClientBundle\Entity\Country", cascade={"persist"}, orphanRemoval=true)
 * @ORM\JoinTable(name="offer_country",
 *      joinColumns={@ORM\JoinColumn(name="offer_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="country_id", referencedColumnName="id")}
 *      )
 * */
private $countries;

...

/**
 * This is method getCountries
 *
 * @return ArrayCollection 
 *
 */
public function getCountries()
{
    return $this->countries;
}

In the form type, here's how the multiple select box is created:

->add('countries', 'entity', [
                'class' => 'ClientBundle:Country',
                'property' => 'name',
                'required' => false,
                'expanded' => false,
                'multiple' => true,
                'mapped' => true
            ])

In the controller, here are the functions having to do with editing:

/**
 * Displays edit form
 * 
 * @throws NotFoundHttpException
 * 
 * @param Request $request
 * @param int $id
 */
public function editAction(Request $request, $id)
{
    $em = $this->getDoctrine()->getManager();
    $offer = $em->getRepository('ClientBundle:Offer')->find($id);

    if (!$offer)
    {
        throw new NotFoundHttpException('Offer not found!');
    }

    $this->setEditForm($offer, '/offer/editPost/' . $id);

    return [
        'form' => $this->form->createView(),
        'devicesForm' => $this->getDevicesForm('/devices/listJson')->createView(),
        'gate' => $this->gate
    ];
}

/**
 * Handles submission of edit form
 */
public function editPostAction(Request $request, $id)
{
    $em = $this->getDoctrine()->getManager();
    $offer = $em->getRepository('ClientBundle:Offer')->find($id);

    if (!$offer)
    {
        throw new NotFoundHttpException('Offer not found!');
    }

    $this->setEditForm($offer);
    $this->form->handleRequest($request);

    if (!$this->form->isValid())
    {
        throw new InvalidArgumentException('Unable to save this item due to invalid argument(s).');
    }

    return ['form' => $this->form->createView()];
}

/**
 * Sets up $this->form as a new edit form with the specified Offer
 * 
 * @param Offer $offer
 */
protected function setEditForm($offer, $action = '')
{
    $this->setOffer($offer);
    $this->form = $this->createForm(new OfferType, $this->offer, ['action' => $action]);
    $this->form->add('submit', 'submit');
}

/**
 * Sets $this->offer
 * @param Offer $offer
 */
protected function setOffer(Offer $offer = null)
{
    if (!$offer)
    {
        $offer = new Offer;
    }

    $this->offer = $offer;
}
  • 写回答

1条回答 默认 最新

  • douhunbei0166 2014-09-25 03:17
    关注

    editPostAction is missing the following:

    $em->persist($this->offer);
    $em->flush();  
    

    It works now. I hope that helps someone else missing one or both of those calls in the future.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 被蓝屏搞吐了,有偿求帮解答,Ai回复直接拉黑
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并