0

I have a submit form using EntityType from another entity class, the two entities don't have any relation between them. In my form class this is the field

->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',
])

When submitting, i get only the pin from the user object, and store it in the database. Now i want to make edit page, where i use this "pin" value to retrieve the name of the user

When i try to load the object into the form and render it, the selected value is the placeholder default value not the actual value from the database.

1
  • 1
    What did you tried to do for edit? show the code for edit for edit page in form and controller. Commented Oct 31, 2022 at 9:41

1 Answer 1

1

For everyone trying to reuse form with dropdown menu from another table without relations in 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(); 
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.