Save as copy in Symfony

1

I'm trying to create a form that has two buttons, save as a copy and save and overwrite.

My affected entities in the form are: Checklist-> ChecklistGroup-> ChecklistGroupTask-> ChecklistGroupTaskCorrective

Until now I have been able to control that when I save as a copy the new entities are generated only in the copy and leave the original intact, but I'm not getting it to work when I delete.

This is my code:

public function EditAction($id, Request $request)
{
    $auth_checker = $this->get('security.authorization_checker');
    $token = $this->get('security.token_storage')->getToken();
    $user = $token->getUser();
    $em = $this->getDoctrine()->getManager();
    $checklist = $em->getRepository('AppBundle:Checklist')->find($id);

    if (!$checklist) {
        throw $this->createNotFoundException('No se ha encontrado el checklist de id ' . $id);
    }

    $originalGroups = new ArrayCollection();
    $originalTasks = new ArrayCollection();
    $originalCorrectives = new ArrayCollection();

    /** @var ChecklistGroup $group */
    foreach ($checklist->getGroups() as $group) {
        $originalGroups->add($group);
        /** @var ChecklistTask $task */
        foreach ($group->getTasks() as $task) {
            $originalTasks->add($task);
            foreach ($task->getCorrectives() as $corrective) {
                $originalCorrectives->add($corrective);
            }
        }
    }

    $editForm = $this->createForm(ChecklistType::class, $checklist);

    $editForm->handleRequest($request);

    if ($editForm->isValid()) {
        $checklist->setUserModificator($user);
        $checklist->setEstado(true);

        if ($editForm->get('Save')->isClicked()) {
            $em->persist($checklist);
            $em->flush();
        }

        if ($editForm->get('SaveCopy')->isClicked()) {
            $copy = clone $checklist;
            $copy->setNombre($checklist->getNombre() . ' #COPY' . substr(uniqid(), 8, 4) . '#');
            $copy->clearId();

            /** @var ChecklistGroup $newGroup */
            foreach ($copy->getGroups() as $newGroup) {
                $newGroup->setChecklist($copy);
                /** @var ChecklistTask $newTask */
                foreach ($newGroup->getTasks() as $newTask) {
                    $newTask->setChecklistgroup($newGroup);
                    /** @var ChecklistTaskCorrective $newCorrective */
                    foreach ($newTask->getCorrectives() as $newCorrective) {
                        $newCorrective->setChecklistTask($newTask);
                    }
                }
            }

            $em->persist($copy);
            $em->detach($checklist);
            $em->flush();
        }
        $this->get('session')->getFlashBag()->add('success', "Se ha actualizado el Checklist correctamente.");

        return $this->redirectToRoute('listChecklist', array('id' => $id));
    }

    return $this->render(':checklist:create.html.twig', array(
        'edit' => true,
        'form' => $editForm->createView(),
    ));
}

I'm going crazy with this little problem ... any suggestion is welcome.

    
asked by Kynethix 30.06.2016 в 09:52
source

1 answer

1

In order to save a data before it is edited you can use the function 'prePersist' and 'preUpdate'. Since they are executed before storing in the database. You can save the current data in another table / entity. I give you an example that would go in the Admin:

public function preUpdate($entity)
{
    $this->prePersist($entity);
}

public function prePersist($entity){
    $container = $this->getConfigurationPool()->getContainer();

    $i18nRepo = $container->get('doctrine')->getRepository("AppBundle:xxx");

    $antiguo = $entity->getAntiguo();

    $buscamos_dato = $i18nRepo->findCa();
    $antiguo->setDato($buscamos_dat);
}
    
answered by 11.08.2016 в 14:26