我有一個使用來自另一個物體類的 EntityType 的提交表單,這兩個物體之間沒有任何關系。在我的表單類中,這是該欄位
->add('user', EntityType::class, [
'class'=>User::class,
'query_builder' => function(UserRepository $userRepository){
$db = $userRepository->createQueryBuilder('u');
$db->andWhere($db->expr()->isNotNull('u.name')); // get users with name
return $db;
},
'choice_label' => 'name',
'choice_value' => 'pin',
'placeholder' => 'Choose owner',
])
提交時,我只從用戶物件中獲取 pin,并將其存盤在資料庫中。現在我想制作編輯頁面,在這里我使用這個“pin”值來檢索用戶的名字
當我嘗試將物件加載到表單中并呈現它時,所選值是占位符默認值,而不是資料庫中的實際值。
uj5u.com熱心網友回復:
對于每個試圖在 symfony6 中沒有關系的另一個表中重用帶有下拉選單的表單的人
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('pin', ChoiceType::class, [
'choices'=> $this->getUsers(),
])
}
public function getUsers(){
$conn = $this->getEntityManager()->getConnection();
$query = "SELECT `name`, `pin` FROM `user` where `name` is not null order by `name` ";
$stmt = $conn->executeQuery($query);
return $stmt->fetchAllKeyValue();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/525712.html
標籤:php交响乐
