[英]How to use ParamConverter with Doctrine and Symfony2
我知道我可能错过了一些非常简单的东西,但我只是不知道是什么,而且我仍然是symfony的新手。
我想要一个像/admin/user/edit/1234
的网址,其中控制器使用paramConverter从他们的ID自动加载用户。 但我不断收到错误。
Some mandatory parameters are missing ("id") to generate url for route.
我试过关注http://symfony.com/doc/current/best_practices/controllers.html,但它的工作原理并不是很详细。
这是我的路线
admin_edit_user:
pattern: /users/edit/{id}
defaults: {_controller: MyBundle:AdminUsers:editUser}
requirements:
id: \d+
这是我的控制器
<?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
));
}
}
不理我! 我发现了这个问题。 当我为表单生成URL时,我缺少所需的参数。
$userInfoForm = $this->createForm(new UserInfoType() , $info , array(
'action' => $this->generateUrl('admin_edit_user')
));
需要
$userInfoForm = $this->createForm(new UserInfoType() , $info , array(
'action' => $this->generateUrl('admin_edit_user' , array('id' => '1234'))
));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.