我正在使用symfony2 CRUD生成器来管理用户。当我更新数据库中未更新的用户密码时,发生非常罕见的事件。但是,当我使用名称或电子邮件等任何其他字段更新用户密码时,密码会更新。
我的UserType Form类位于
之下class UserType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('email')
->add('plainPassword','password')
->add('roles','choice',array('choices'=>array('ROLE_ADMIN'=>'Admin','ROLE_MEMBER'=>'Member') ,'multiple' => true,'expanded'=>true))
->add('path','hidden')
->add('file');
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Ars\PlaybookBundle\Entity\User'
));
}
/**
* @return string
*/
public function getName()
{
return 'ars_playbookbundle_user';
}
}
我的crud控制器更新操作看起来像
/**
* Edits an existing User entity.
*
* @Route("/{id}", name="user_update")
* @Method("PUT")
* @Template("ArsPlaybookBundle:User:edit.html.twig")
*/
public function updateAction(Request $request, $id) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ArsPlaybookBundle:User')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find User entity.');
}
$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createEditForm($entity);
$email=$request->request->all();
$password = $email['ars_playbookbundle_user']['plainPassword'];
if(isset($password)){
$editForm->get('plainPassword')->setData($password);
}
$editForm->handleRequest($request);
//\Doctrine\Common\Util\Debug::dump($editForm->getData()); die;
if ($editForm->isValid()) {
$entity->upload();
$em->flush();
$this->get('session')->getFlashBag()->add(
'notice', 'Your changes were saved!'
);
return $this->redirect($this->generateUrl('user_edit', array('id' => $id)));
}
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
答案 0 :(得分:0)
最后,我通过使用用户管理器而不是form或entityManager来实现它。现在我只能更新密码字段而不更改任何其他字段。