我在Symfony中創建了一個用戶資料頁面。當我點擊個人資料時,我需要看到用戶名稱、電子郵件和用戶影像網址。除了用戶名稱之外,一切都能正常作業。沒有顯示name,而是再次顯示email......。就像你在圖片中看到的那樣。當我試圖改變一些東西并保存改變的內容時,所有的東西都被改變了,并被保存為假定的內容,但同樣只有電子郵件可見。 組態檔截圖
這是我在Controller中為profile route撰寫的代碼:
//==
//================= 我的資料 ====
//==
#[Route('/profile/{id}', name: 'profile')]/span>
public function profile($id, 請求$request)。) Response: Response
{
$user = $this->getDoctrine()->getRepository(User::class)-> find($id) 。
$form = $this-> createFormBuilder($user)
->添加("user_name", TextType:: class, array('attr' => array("class" => "form-control fw-light border-1 border-muted rounded-pill bg-light shadow-sm mt-3 text-muted", "style"=> "margin-bottom: 15px")))
->add("email", TextType:: class, array('attr' => array("class" /span> => "form-control fw-light border-1 border-muted rounded-pill bg-light shadow-sm mt-3 text-muted", "style"=> "margin-bottom: 15px")))
->add("user_image", TextType:: class, array('attr' => array("class" /span> => "form-control fw-light border-1 border-muted rounded-pill bg-light shadow-sm mt-3 text-muted", "style"=> "margin-bottom: 15px")))
->add("save", SubmitType:: class, array('attr' => array("class" => "btn-outline-primary fw-light btn-sm border-1 shadow-sm rounded-pill m-3", "style"/span> => "margin-bottom: 15px"), "label" => "Save changes"))->getForm()。
$form-> handleRequest($request)。
if ($form->isSubmitted() && $form->isValid() ) {
$user_name = $form["user_name"]->getData()。
$email = $form["email"]->getData()。
$user_image = $form["user_image"]->getData()。
$user->setUserName($user_name)。
$user->setEmail($email)。
$user->setUserImage($user_image)。
$em = $this->getDoctrine()->getManager()。
$em->persist($user)。
$em->flush()。
$this->addFlash('notice', 'Profile Edited') 。
return $this->redirectToRoute('吃飯')。
}
return $this->render(' meal/profile.html.twig',
["form" => $form-> createView()]
);
}
//===顯示所有用戶===
#[Route('/manageusers', name: 'manageusers')]
public function manageusers(): Response
{
$user = $this->getDoctrine()->getRepository('App:User')->findAll()。
return $this->render(' meal/manageusers.html. twig', array('user' => $user))。)
}
而這是我在實際的Profile twig檔案中的代碼:
{% extends 'base.html.twig' %}{% block title %}My Profile twig
{% block title %}My Profile
{% endblock %}}我的資料
{% block body %}{% block body %}
< h1 class="page-header fw-light text-center py5">
編輯 組態檔
</h1>
<div class="container" >
< div class="row d- flex justify-content-center" >
< div class="col12 col-sm-10 col-md-8 col-lg-6 ">。
<div class="form mb-4" >
{{ form_start(form) }}}.
{{ form_widget(form) }}}
{{ form_end(form) }}}
</div>
</div>
</div>
</div>
{% endblock %}}
uj5u.com熱心網友回復:
在你的用戶物體中,你可能有一個這樣的塊:
/**.
* 一個代表這個用戶的視覺識別符號。
*
* @see UserInterface
*/
public function getUsername()。string
{
return (string) $this-> email;
}
你可以嘗試創建一個這樣的函式
public function getRealUserName() 。string
{
return $this-> userName;
}
然后調整你的表單,或者修改(但要關心認證問題)你的getUsername()函式
。轉載請註明出處,本文鏈接:https://www.uj5u.com/net/310769.html
標籤:
