symfony - 无法更新现有的 Doctrine 实体

标签 symfony symfony-forms

我正在使用 symfony 2.7 表单组件来更新名为 TapsAlert 的实体。
问题是提交表单后实体没有更新。
我不知道出了什么问题。这是部分代码:

public function editarRegistroAction(Request $request, $id){

    $em = $this->getDoctrine()->getManager();
    $altd = $em->getRepository('ModeloBundle:TapsAlert')->find($id);

    $altd->setAsunto($altd->getAsunto());
    $altd->setGls($altd->getGls());
    $altd->setDestinatario($altd->getDestinatario());
    $altd->setPrts($altd->getPrts());
    $altd->setTags($altd->getTags());
    $altd->setValor($altd->getValor());

    $form = $this->createFormBuilder($altd)
        ->add('Asunto', 'text')
        ->add('gls', 'text')
        ->add('destinatario', 'text')
        //->add('dias', 'number')
        ->add('prts', 'text')
        ->add('Tags', 'text')
        ->add('valor', 'text')
        ->add('save', 'submit', array('label' => 'Guardar Cambios'))
        ->getForm();

        //$form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {

            //$edAlert = $form->getData();
            $em = $this->getDoctrine()->getManager();
            //$em->persist($altd);
            $em->flush();

            return $this->redirect('http://192.168.1.128/');
        }

    return $this->render('MotoBundle:Default:edit.html.twig', array('form' => $form->createView()));
}

提前致谢。

最佳答案

第一:这部分代码没有用。应将其删除:

$altd->setAsunto($altd->getAsunto());
$altd->setGls($altd->getGls());
$altd->setDestinatario($altd->getDestinatario());
$altd->setPrts($altd->getPrts());
$altd->setTags($altd->getTags());
$altd->setValor($altd->getValor());

第二:您没有处理您的请求:

  public function editarRegistroAction(Request $request, $id){
        $em = $this->getDoctrine()->getManager();
        $altd = $em->getRepository('ModeloBundle:TapsAlert')->find($id);

        $form = $this->createFormBuilder($altd)
            ->add('Asunto', 'text')
            ->add('gls', 'text')
            ->add('destinatario', 'text')
            //->add('dias', 'number')
            ->add('prts', 'text')
            ->add('Tags', 'text')
            ->add('valor', 'text')
            ->add('save', 'submit', array('label' => 'Guardar Cambios'))
            ->getForm();

            $form->handleRequest($request); //uncomment this line
            if ($form->isSubmitted() && $form->isValid()) {
                $em->flush();

                return $this->redirect('http://192.168.1.128/'); // This url should an be an external url
            }

        return $this->render('MotoBundle:Default:edit.html.twig', array('form' => $form->createView()));
    }

第三:我相信你可以使用 paramConvertor 改进你的代码。 :

  public function editarRegistroAction(Request $request, TapsAlert $tapsAlert){

        $form = $this->createFormBuilder($tapsAlert)
            ->add('Asunto', 'text')
            ->add('gls', 'text')
            ->add('destinatario', 'text')
            //->add('dias', 'number')
            ->add('prts', 'text')
            ->add('Tags', 'text')
            ->add('valor', 'text')
            ->add('save', 'submit', array('label' => 'Guardar Cambios'))
            ->getForm();

            $form->handleRequest($request);
            if ($form->isSubmitted() && $form->isValid()) {
                $this->getDoctrine()->getManager()->flush();

                return $this->redirect('http://192.168.1.128/'); // This url should an be an external url
            }

        return $this->render('MotoBundle:Default:edit.html.twig', array('form' => $form->createView()));
    }

在单独的类中构建表单也是一个更好的做法,有关更多详细信息,请查看官方文档 https://symfony.com/doc/current/forms.html#creating-form-classes

关于symfony - 无法更新现有的 Doctrine 实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53726532/

相关文章:

php - Symfony $form->getData() 返回值连接的空对象

Symfony2 ObjectNormalizer 非规范化回调

Symfony 总是返回拒绝访问、@Security、 Controller 、isAuthor

php - 将参数从实体传递到 query_builder

php - 如何将默认选项与 Symfony 表单中的新选项合并

php - 手动提交 Symfony 表单后 CollectionType 字段显示为空

php - Symfony2 - 创建一个 Doctrine 过滤器来选择当前用户数据

php - Doctrine MIN() 低性能

symfony - 在 Symfony2 中删除/取消设置表单字段

symfony - 带有 propel 的 symfony2 表单的实体字段类型等效是什么?