M
M
Maxim Lagoysky2019-05-28 11:29:07
symfony
Maxim Lagoysky, 2019-05-28 11:29:07

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

1 answer(s)
A
Alexander, 2019-05-28
@lagoy

Why are you not satisfied with Many-to-Many ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question