O
O
ol sm2017-08-12 23:25:27
symfony
ol sm, 2017-08-12 23:25:27

Symfony3 How to write to two tables in one controller?

Goodnight.
The situation is as follows, there is a FOSUserBundle registration, there are 2 tables users and info_user.
How it is possible to write down the data at once in two tables of the form for users:
- username;
- password;
- email;
info_user form:
- lastname;
- firstname;
- phone;
Thanks for the help, using symfony3.3.5

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Skobkin, 2017-08-13
@skobkin

https://symfony.com/doc/current/doctrine/associati...

O
ol sm, 2017-08-13
@ol_efk

public function createAction(Request $request)
    {

        $individualUser = new IndividualUser();

        $iform = $this->createForm('BackendBundle\Form\IndividualUserType', $individualUser);

        $iform->handleRequest($request);

        $user = new User();

        $form = $this->createForm('BackendBundle\Form\UserType', $user);

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $individualUser->setUser($user->getId());
            $em->persist($individualUser);
            $em->persist($user);
            $em->flush();

            return $this->redirectToRoute('_user_index');
        }

        return $this->render('BackendBundle:User:create.html.twig',array(
            'form' => $form->createView(),
            'iform' => $oform->createView()
        ));
    }

An exception occurred while executing 'INSERT INTO individualuser (firstname, lastname, phone, user_id) VALUES (?, ?, ?, ?)' with params [null, null, null, null]: Please
correct where I went wrong.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question