Symfony 5 – Get old collection data before submit form

In my symfony 5 project I would like when submitting a certain form, compare the entity before and after submission.

So keep a copy of the original entity in order to perform processing.

I’ve :

$parametresAdmin = $entrepriseService->getParametresAdmin();

        $form = $this->createForm(ParametresAdminType::class, $parametresAdmin, [
            'entreprise' => $this->getUser()->getEntreprise(),
        ]);

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {

            $entrepriseService->updateParametres($parametresAdmin);

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

In fact, I want to get a copie of $parametresAdmin->getTypesConges() (which is a collection on OneToMany).

So, when the form is submitted, I want compare the old $parametresAdmin->getTypesConges() and the new $parametresAdmin->getTypesConges().

I tried a lot of things like

        $parametresAdmin = $entrepriseService->getParametresAdmin();
        $oldParametresAdmin = clone $parametresAdmin;

        $form = $this->createForm(ParametresAdminType::class, $parametresAdmin, [
            'entreprise' => $this->getUser()->getEntreprise(),
        ]);

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {

            dd($oldParametresAdmin->getTypesConges(), $parametresAdmin->getTypesConges());

            $entrepriseService->updateParametres($parametresAdmin);
    //...

But each time, I can’t get back to the initial state of $parametresAdmin->getTypesConges().
I’m lost … could someone help me? Thank you

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *