2016-09-25 56 views
0

我正在嘗試編輯我的用戶,並且在提交時我沒有遇到任何錯誤,但用戶未在數據庫中更新。CRUD,手動編輯用戶FOSUserBundle Symfony

我想知道,如果管理員在用戶(其他字段,而不是密碼)進行了一些更改,它會堅持用戶使用舊密碼,如果管理員更改密碼,它會堅持最新的密碼: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') 
    ; 
} 

回答

0

您必須使用FosUserBundle Manager服務,根據此:Fos User Manager Service

+0

你有時間測試嗎? –

+0

你能告訴我們怎麼回事? –