【问题标题】:Trying to update OR insert in Symfony2 controller with a form尝试使用表单更新或插入 Symfony2 控制器
【发布时间】:2012-04-09 17:08:21
【问题描述】:

我的代码有什么问题?我想根据 url 更新或插入 Moteur 对象。
提前致谢。

/**
* @Route("/moteur/{moteurid}", name="moteur", requirements={"moteurid" = "\d+"}, defaults={"moteurid" = null})
* @Template()
*
* Cette page permet d'enregistrer de nouveaux moteurs (et de les éditer).
*/
public function moteurAction($moteurid)
{
    $args=array();
    $avertissement = null;
    if (!$this->get('security.context')->isGranted('ROLE_ADMIN'))
    {
        $avertissement = "Vous n'avez pas le droit d'accéder à cet espace.";
        return $this->redirect($this->generateUrl('index', array('avertissement' => $avertissement)));
    }

    $args['menu']['admin'] = 'selected';
    $obj = null;
    if ($moteurid == null)
    {
        $obj = new Moteur();
    }
    else
    {
        $obj = $this->getDoctrine()->getRepository('CreasixtineAFBundle:Moteur')->find($moteurid);
    }
    $form = $this->createForm(new FormMoteur(), $obj);
    $args['form'] = $form->createView();

    if ($this->getRequest()->getMethod() == 'POST')
    {
        $form->bindRequest($this->getRequest());

        if ($form->isValid())
        {
            $obj = $form->getData(); // Type Moteur()
            $pn = $obj->getPnid();

            $em = $this->getDoctrine()->getEntityManager();
            if ($moteurid == null)
            {
                $em->persist($obj);
                $avertissement = "Moteur créé !";
            }
            else 
            {
                // Rien, le moteur sera mis à jour avec flush()
                $avertissement = "Moteur mis à jour !";
            }
            foreach ($pn as $my_pn){$em->persist($my_pn);}
            $em->flush();

            return $this->redirect($this->generateUrl('admin', array('avertissement' => $avertissement)));
        }
        else
        {
            throw new Exception("Le formulaire n'est pas valide.");
        }
    }

    $contenu = $this->rendu($args, "formulaire_moteur.html.twig");
    return $contenu;
}

【问题讨论】:

    标签: forms symfony doctrine controller insert-update


    【解决方案1】:

    首先,您不需要这一行,因为 PHP5 本身通过引用传递对象:

    $obj = $form->getData(); // Type Moteur()
    

    那么,你在 Moteur 和 Pn 之间的关系有点混乱。你用 getPnid() 得到了一个 Pn,但你得到了一个你想要持久化的对象?

    无论如何,这些 Pn 对象应该在 Moteur 之前被持久化,所以这是我要写的:

    if ($form->isValid())
    {
        $em = $this->getDoctrine()->getEntityManager();
    
        $pn = $obj->getPnid();
    
        //Persist these related objects BEFORE Moteur
        foreach ($pn as $my_pn)
        {
            $em->persist($my_pn);
        }
    
        if ($moteurid == null)
        {
            $em->persist($obj);
            $avertissement = "Moteur créé !";
        }
        else 
        {
            // Rien, le moteur sera mis à jour avec flush()
            $avertissement = "Moteur mis à jour !";
        }
        $em->flush();
    
        return $this->redirect($this->generateUrl('admin', array('avertissement' => $avertissement)));
    }
    else
    {
        throw new Exception("Le formulaire n'est pas valide.");
    }
    

    【讨论】:

    • 但是如果我想让所有相关实体都带有 "cascade: [all]" 怎么办?我不能让它以这种方式工作,我得到“SQLSTATE [23000]:完整性约束违规:1062 Duplicate entry”。
    猜你喜欢
    • 2010-10-04
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    • 2018-01-27
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多