donglingsai2880 2016-10-31 09:49
浏览 39

Symfony 3.1:编辑表单创建新的数据库条目而不是更新

I'm having problems updating a form containing two entities ("Event" and "Info) in symfony. Everything works fine right up until the user tries to update the information. When they do it's all saved as new database entries.

public function editAction(Request $request, Event $event)
{
    $user = $this->getUser();

    $editForm = $this->createFormBuilder()
        ->setMethod('PUT');      

    $editForm->add("event", 'AppBundle\Form\EventType');
    $editForm->add("info", 'AppBundle\Form\InfoType');

    $editForm = $editForm->getForm();
    $editForm->handleRequest($request);

    if ($editForm->isSubmitted() && $editForm->isValid()) {
       $em = $this->getDoctrine()->getManager();
       $formEvent = $editForm->get('event')->getData();

       echo "Debug<br />";
       echo "form id: ". $formEvent->getId(); // Wrong. Returns null.
       echo "<br /> event id: ". $event->getId(); // The correct id of entity that should be updated.

       $em->merge($formEvent);
       $em->flush();       
    } else {
       $editForm->get('event')->setData($event);
    }

    return $this->render('event/edit.html.twig', array(
        'event' => $event,
        'edit_form' => $editForm->createView()
    ));  

I echo some debug information, and handleRequest() do map the new information into the event-entity found in $editForm->get('event') with the exception that it's Id is missing which I assume has something to do with the creation of the new entity instead of updating. What's up?

Routing: event_edit: path: /{id}/edit defaults: { _controller: "AppBundle:Event:edit" } methods: [GET, POST, PUT]

Twig:

{{ form_start(edit_form, {'attr': {'novalidate': 'novalidate'}}) }}
    {{ form_errors(edit_form) }}
    {{ form_widget(edit_form.event.title) }}
    {{ form_widget(edit_form.info) }}
    <input type="submit" value="Save" />
{{ form_end(edit_form) }}
  • 写回答

1条回答 默认 最新

  • duancuo1234 2016-10-31 09:53
    关注

    If event entity is the entity that you would like to update and it managed by doctrine(was loaded), you need to pass event entity to your form. Try this:

    public function editAction(Request $request, Event $event)
    {
        $user = $this->getUser();
    
        $editForm = $this->createFormBuilder($event)
            ->setMethod('PUT');      
    
        $editForm->add("event", 'AppBundle\Form\EventType');
        $editForm->add("info", 'AppBundle\Form\InfoType');
    
        $editForm = $editForm->getForm();
        $editForm->handleRequest($request);
    
        if ($editForm->isSubmitted() && $editForm->isValid()) {
           $em = $this->getDoctrine()->getManager();
           $em->persist($event);
           $em->flush();       
        }
    
        return $this->render('event/edit.html.twig', array(
            'event' => $event,
            'edit_form' => $editForm->createView()
        ));  
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算