我想更新用户实体(使用唯一的用户ID选择)。
它应该看起来像一个表(宽度= 3个字段),左边是列名(名字,姓氏等),中间是当前值,右边是新值的输入字段
这是我在edit.html.twig中使用div创建的当前表:
<div>
<div>
<span></span>
<span>Current</span>
<span>Edit here</span>
</div>
<div>
<span>Emailadress:</span>
<span>{{ userById.getEmail }}</span>
<span></span>
</div>
<div>
<span>First name:</span>
<span>{{ userById.getFirstname }}</span>
<span></span>
</div>
<div>
<span>Last name:</span>
<span>{{ userById.getLastname }}</span>
<span></span>
</div>
</div>
现在出现了问题。在最后一个跨度内,我想要输入字段来编辑值。我尝试使用一个用值填充实体的表单,并将其与名称字段和当前字段组合。
我尝试将一些Editfields插入到下面的Controller函数中,但没有让它工作:(
目前我选择了用户ID并使用它构建表单,将值传递给我的编辑视图。
仅丢失输入字段,显示当前数据正常。以下是我的编辑功能:
public function editUserAction($id, Request $request) {
$em = $this->getDoctrine()->getManager();
$userId = $em->getRepository('CompanyUserBundle:User')->find($request->get('id'));
$form = $this->createForm(new UserForm($request->getSession()), new User(), array(
'action' => $this->generateUrl('backend_user_management_edit', array('id' => $userId->getId())),
'em' => $em)
);
$form->handleRequest($request);
$userData = $this->getDoctrine()
->getRepository('CompanyUserBundle:User')
->findOneById($userId);
if (!$userData) {
throw $this->createNotFoundException(
'No user found for id '.$userId
);
}
return $this->render(
'CompanyUserBundle:User:edit.html.twig',
array(
'form' => $form->createView(),
'action' => $this->generateUrl('backend_user_management_edit', array('id' => $userId->getId())),
'userById' => $userId
)
);
}
如何添加输入字段以编辑值并使用新值更新当前用户?
亲切的问候
答案 0 :(得分:0)
控制器
public function editUserAction($id, Request $request) {
$em = $this->getDoctrine()->getManager();
$userId = $request->get('id');
$user = $this->getDoctrine()
->getRepository('CompanyUserBundle:User')
->findOneById($userId);
if (!$userData) {
throw $this->createNotFoundException(
'No user found for id '.$userId;
);
}
$form = $this->createForm(new UserForm($request->getSession()), $user, array(
'action' => $this->generateUrl('backend_user_management_edit', array('id' => $userId->getId())),
'em' => $em)
);
$form->handleRequest($request);
return $this->render(
'CompanyUserBundle:User:edit.html.twig',
array(
'form' => $form->createView(),
'action' => $this->generateUrl('backend_user_management_edit', array('id' => $userId->getId())),
'userId' => $userId,
'user' => $user
)
);
}
查看
{{ form_start(form) }}
<div>
<div>
<span></span>
<span>Current</span>
<span>Edit here</span>
</div>
<div>
<span>Emailadress:</span>
<span>{{ user.email }}</span>
<span>{{ form.email }}</span>
</div>
<div>
<span>First name:</span>
<span>{{ user.firstname }}</span>
<span>{{ form.firstname }}</span>
</div>
<div>
<span>Last name:</span>
<span>{{ user.getLastname }}</span>
<span>{{ form.lastname }}</span>
</div>
<input type="submit" />
</div>
{{ form_end(form) }}