更新方法出错

时间:2015-12-22 12:54:33

标签: symfony

我在miseAjour方法中有错误,我无法找到解决方案

这是错误: Controller" Team \ ManagerBundle \ Controller \ PlayerController :: miseAjourAction()"要求您为" $ id"提供价值。参数(因为没有默认值或因为在此之后存在非可选参数)。

这里是miseAjourAction的代码:

public function miseAjourAction($ id){

$em = $this->container->get('doctrine')->getEntityManager();
$Player = $em->getRepository('TeamManagerBundle:Player')->find($id);
$form = $this->createform(new PlayerForm(),$Player);

$request = $this->getRequest();

if($request->getMethod() == 'POST'){
    $form->bind($request);
    if($form->isValid()){

        $em = $this->container->get('Doctrine')->getEntityManager();
        $em->persist($Player);
        $em->flush();


        return $this->render('TeamManagerBundle:Player:succes.html.twig',array('msg'=>'Mise à jour effectué avec succés'));
    }
}
return $this->render('TeamManagerBundle:Player:miseajour.html.twig',array('Form'=>$form->createView()));


    }

1 个答案:

答案 0 :(得分:0)

问题可能与上一篇文章相同:最好在您的存储库中使用findOneById()(如果没有结果,则抛出异常)。尝试使用这种代码并告诉我们它是否能解决您的问题。

public function miseAjourAction($id)
{
    $em = $this->getDoctrine()->getEntityManager();

    $player = $em->getRepository('TeamManagerBundle:Player')->findOneById($id);

    if (!$player) {
        throw $this->createNotFoundException('No player found for id '.$id);
    } 

    $form = $this->createform(new PlayerForm(), $player);

    $form->handleRequest($request);

    if ($form->isValid()) {
        $em->persist($player);
        $em->flush();

        return $this->render('TeamManagerBundle:Player:succes.html.twig',array('msg'=>'Mise à jour effectué avec succés'));
    }

    return $this->render('TeamManagerBundle:Player:miseajour.html.twig',array('Form'=>$form->createView()));
}