dougu5950 2015-02-16 17:44
浏览 21
已采纳

如何使用ParamConverter与Doctrine和Symfony2

I know I'm probably missing something really easy, but I just don't know what, and I'm still pretty new to symfony.

I'd like to have a url like /admin/user/edit/1234 where the controller uses the paramConverter to auto load the user from their ID. But I keep getting an error.

Some mandatory parameters are missing ("id") to generate url for route.

I tried following http://symfony.com/doc/current/best_practices/controllers.html but it's not very detailed in how it all works.

Here's my route

admin_edit_user:
    pattern: /users/edit/{id}
    defaults: {_controller: MyBundle:AdminUsers:editUser}
    requirements:
        id: \d+

And here's my controller

<?php
//...
    class AdminUsersController extends Controller
    {
        //... 
        public function editUserAction(Request $request , User $user)
        {
            $info = $user->getInfo();

            if(is_null($info))
            {
                $info = new UserInfo();
            }

        $userInfoForm = $this->createForm(new UserInfoType() , $info , array(
        'action' => $this->generateUrl('admin_edit_user')
    ));

        $userInfoForm->handleRequest($request);

        if($userInfoForm->isValid())
        {
            if(is_null($user->getInfo()))
            {
                $user->setInfo($info);
            }

            $em = $this->getDoctrine()->getManager();
            $em->persist($info);
            $em->persist($user);
            $em->flush();
            $request->getSession()->getFlashBag()->add('notice' , 'User Info succesfully updated.');
        }

        return $this->render('MyBundle:Admin/Users:edit.html.twig' , array(
            'user_info_form' => $userInfoForm->createView(),
            'user' => $user
        ));
    }
}
  • 写回答

1条回答 默认 最新

  • dounei9043 2015-02-16 18:27
    关注

    Ignore me! I found the issue. When I'm generating the URL for the Form I was missing the parameters needed.

    $userInfoForm = $this->createForm(new UserInfoType() , $info , array(
        'action' => $this->generateUrl('admin_edit_user')
    ));
    

    Needed to be

    $userInfoForm = $this->createForm(new UserInfoType() , $info , array(
        'action' => $this->generateUrl('admin_edit_user' , array('id' => '1234'))
    ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么