Answer the question
In order to leave comments, you need to log in
How to save data from another entity?
Hello everyone, in general there are 3 tables, (user, project, user_projects), the user_project table consists of 3 fields id, user_id, project_id. I created a form for creating a user, added a select from the list of projects, i.e. the select uses Project::class, so when saving, I naturally catch an error that UserProject is expected, and I give Project, so the question is how to do it right? I saw there are data converters, but I'm not sure if this is the case where they should be used.
$builder->add('projects', EntityType::class, array(
'class' => (string) Project::class,
'attr' => array('class' => 'form-control listbox-no-selection listbox-tall'),
'multiple' => true,
'choice_label' => 'name',
'required' => false,
))
if ($request->isMethod('POST')) {
$form->submit($request->request->get($form->getName()));
if ($form->isSubmitted() && $form->isValid()) {
/** @var $user User */
$user = $form->getData();
$user->setPassword($passwordEncoder->encodePassword($user, $user->getPassword()));
$em->persist($user);
$em->flush();
return $this->redirectToRoute('users_list');
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question