Home  >  Q&A  >  body text

Create a new instance of an entity object that does not conform to the formal result - Symfony 5

I have three entities. "Bordereau", "Stagiaire", "BordereauStagiaire".

I have created a multi-step form where I create a new Bordereau and I need to show the user the selected "Stigiaires" and then validate it. Both Bordereau and Stagiaire are linked through the table BordereauStagiaire.

I managed to get the Bordereau and Stagiaire ids in the controller via the new Bordereau form. But I'm looking for a way to create a new BordereauStagiaire when a new Bordereau is created.

This is the method I have started to implement. Backend, the Bordereau will be created first, then the BS will be created with the new Bordereau and the selected stagiaires. The BS table has Bordereau id, Stagiaire id and amount (another attribute).

How do I create a new BordereauStagiaire using the Bordereau's ID and the IDs of the Stagiaires I got from the form?

This is my controller

#[Route('/create/bordereau', name: 'app_bordereau_create', methods: ['GET','POST'])]
    public function createBordereauAction(CreateBordereauFlow $flow, PersistenceManagerRegistry $doctrine)
    {
        $formBordereau = new Bordereau();
        $formBordStag = new BordereauStagiaire();

        $formBordereau->setUser($this->getUser());

        $flow->bind($formBordereau);

        $form = $flow->createForm();
        
        if ($flow->isValid($form)) {
            $flow->getCurrentStepNumber($form);
            $flow->saveCurrentStepData($form);
            if ($flow->nextStep()) {
                $form = $flow->createForm();
                $flow->getCurrentStepNumber($form);
                $flow->saveCurrentStepData($form);
                // dd($form['stagiaire']->getData()[0]->getId());
                // dd($form->getData());
                
            } else{
                
                $entityManager = $doctrine->getManager();
                $entityManager->persist($formBordereau);
                $entityManager->flush();
                // dd($formBordereau->getId());
                return $this->redirectToRoute('app_bordereau_index');
                
            }
        }

        // if ($flow->redirectAfterSubmit($form)) {
        //     // $request = $this->getRequest();
        //     $params = $this->formFlowUtil->addRouteParameters(array_merge($request->query->all(),
        //             $request->attributes->get('_route_params')), $flow);
    
        //     return $this->redirectToRoute($request->attributes->get('_route'), $params);
        // }

        return $this->render('bordereau/create_bordereau.html.twig', [
            'form' => $form->createView(),
            'flow' => $flow,
            'formBordereau' =>$formBordereau,
        ]);
    }

This is how I get the data from the formphp $form['stagiaire']->getData()[0]->getId() and bordereau, I get its id after persisting Flush to the database. php $formBordereau->getId()

P粉004287665P粉004287665376 days ago505

reply all(1)I'll reply

  • P粉771233336

    P粉7712333362023-09-13 09:55:02

    What I wrote was an answer, but it was more like a comment with a lot of text.

    First of all, I recommend that you only use English named variables and at least avoid using bordereau for operation names like create.

    Then, give the variable you will use a name: $formBordereau is not a form, it is an entity and you should only use $bordereau.

    So, do you need this entity BordereauStagiaire? I know this is a ManyToMany relationship, Symfony and Doctrine manage it directly through entities, you don't need this third entity.

    Doctrine is an ORM, which means you can only work with objects and not IDs, if you design the relationship correctly there should be a method addStagiaire in your Bordereau entity, and vice versa Likewise.

    You should then call this method with the following object:

    // I am not sure here, does it return a Stagiaire Entity ? 
    // I don't understand your flow form
    $stagiaire = $form['stagiaire']->getData()[0];
    
    $entityManager->persist($bordereau);
    $bordereau->addStagiaire($stagiaire);
    $entityManager->flush();

    reply
    0
  • Cancelreply