TeslaX93
TeslaX93

Reputation: 97

How can I do INSERT instead of UPDATE in Symfony?

(Symfony-newbie here), I have some basic code that updates fields in DB.

public function editAction(Request $request, $id)
{
 
    
    $em = $this->getDoctrine()->getManager();
    $testowanie = $em->getRepository('TestowanieBundle:Test')->findOneBy(array('id' => $testowanieId));
     
    $form = $this->createForm(TestowanieType::class, $testowanie);
 
    $form->handleRequest($request);
 
 
    if ($form->isSubmitted() && $form->isValid()) {
 
        $em->persist($testowanie);
 
        $em->flush();
        return $this->redirectToRoute('...');
    }

This function gets data from row in DB and overwrites it, but I want to insert a copy with new id based on data currently saved in DB.

Upvotes: 2

Views: 574

Answers (1)

davidbonachera
davidbonachera

Reputation: 5760

How to re-save the entity as another row in Doctrine 2

As stated in this post, you should either reset the id or just use the Clone keyword like this:

if ($form->isSubmitted() && $form->isValid()) {
    $new_entity = clone $testowanie;
    $em->persist($new_entity);
    $em->flush();
    return $this->redirectToRoute('...');
}

Upvotes: 2

Related Questions