CRUD,手动编辑用户FOSUserBundle Symfony

时间:2016-09-25 14:31:08

标签: php symfony fosuserbundle

我正在尝试编辑我的用户,当我提交时我没有收到任何错误,但用户未在数据库中更新。

我想知道,如果管理员在用户(其他字段,而不是密码)中进行了一些更改,它将使用旧密码保留用户,如果管理员更改密码,它将保留最新密码: d

抱歉我的英语,谢谢你们:)

这是我在\ UserController.php中的EditAction

public function editAction(Request $request, User $user)
{                  
    $password = $user->getPassword();

    $deleteForm = $this->createDeleteForm($user);
    $editForm = $this->createForm('UserBundle\Form\EditUserType', $user);
    $editForm->handleRequest($request);


    if ($editForm->isSubmitted() && $editForm->isValid()) {

        if(!empty($form->get('password')->getData())){
            $user->setPlainPassword($form->get('password')->getData());
            }
        else{
        $user->setPassword($password);
            }              
        $em = $this->getDoctrine()->getManager();
        $em->persist($user);
        $em->flush();

        return $this->redirectToRoute('user_edit', array('id' => $user->getId()));
    }

    return $this->render('UserBundle:user:edit.html.twig', array(
        'user' => $user,
        'edit_form' => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    ));
}

这是我的表格\ EditUserType.php

    public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('nom')
        ->add('prenom')    
        ->add('username') 
        ->add('email')
        ->add('password', 'repeated',
        array(
            'type' => 'password',
            'invalid_message' => 'Les mots de passes doivent être identique',
            'required' => false,
            'first_options'  => array('label' => 'Nouveau mot de passe'),
            'second_options' => array('label' => 'Confirmation du mot de passe')
        )
        )                
        ->add('enabled')
        ->add('last_login')
    ;
}

1 个答案:

答案 0 :(得分:0)

您必须使用FosUserBundle Manager服务,根据:Fos User Manager Service