簡體   English   中英

FOSUserBundle編輯用戶無法正常工作

[英]FOSUserBundle edit user not working properly

我正在使用帶有FOSUserBundle的Symfony2,我遇到的問題是編輯用戶帳戶,當我嘗試編輯特定用戶帳戶時,檢索到的用戶信息是正確的但是當我嘗試更新檢索到的用戶信息時,當前記錄的帳戶將是正在編輯的帳戶而不是檢索到的用戶帳戶,怎么可能? 我的代碼出了什么問題?

ProfileController://編輯用戶

public function editUserAction($id, Request $request)
    {
        $em = $this->getDoctrine()->getManager();
        $user = $em->getRepository('MatrixUserBundle:User')->find($id);

        if (!is_object($user)) {
            throw new AccessDeniedException('This user does not have access to this section.');
        }

        /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
        $formFactory = $this->get('fos_user.profile.form.factory');

        $form = $formFactory->createForm(); 
        $form->setData($user); 
        $form->handleRequest($request); 

        if ($form->isValid()) { 
            /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */

            $userManager = $this->get('fos_user.user_manager');
            $userManager->updateUser($user); 

            $session = $this->getRequest()->getSession();
            $session->getFlashBag()->add('message', 'Successfully updated'); 
            $url = $this->generateUrl('matrix_edi_viewUser'); 
            $response = new RedirectResponse($url); 
        }

        return $this->render('FOSUserBundle:Profile:edit.html.twig', array(
            'form' => $form->createView()
        ));
    }

在給定用戶正確更新后,您將返回重定向到自定義路由。

我猜路線matrix_edi_viewUser是原始FOSUB\\ProfileController::showAction

此外,您必須創建特定的showUserAction並將更新的用戶作為參數傳遞給它。
以下代碼應該有效:

public function showUserAction($id)
{
    $user = $em->getDoctrine()
        ->getManager()
        ->getRepository('MatrixUserBundle:User')
        ->find($id);

    if (!is_object($user)) {
        throw new AccessDeniedException('This user does not have access to this section.');
    }

    return $this->render('FOSUserBundle:Profile:show.html.twig', array('user' => $user));
}

路線 :

matrix_edi_viewUser:
    path:     /users/{id}/show
    defaults: { _controller: MatrixUserBundle:Profile:showUser }

在您的editAction中,將重定向更改為:

$url = $this->generateUrl('matrix_edi_viewUser', array('id' => $user->getId()); 
$response = new RedirectResponse($url);

現在,show中顯示的用戶信息將是更新用戶的信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM