Ashish  Kumar Saxena
Ashish Kumar Saxena

Reputation: 4710

why Update and insert in same method is not working in symfony?

I am using symfony3 with window 7: This method should work for both action update and add.But its behavior only insertion. while i am setting $id also.

 /**
     *  @Route("entity/entity/{id}", name="entity_entity",defaults={"id" = 0})    
     */
    public function entityAction(Request $request,$id){
        $action = false;
        $arr_XYZ_data = array();
        $arr_XYZ_prepare_data = array();       
        $form_title = 'Add New XYZ';
        $obj_XYZ = new XYZ();             
        $form = $this->createForm(XYZType::class, $obj_XYZ);
        if($id!=0){
          $obj_repo = $this->getDoctrine()->getRepository('AppBundle:XYZ');
          $arr_XYZ_data = $obj_repo->find($id);         
          if($arr_XYZ_data){
            $action = true;
            $form_title = 'Update XYZ';                   
            $arr_XYZ_data = $obj_repo->findXYZById($id);           
            $arr_XYZ_prepare_data = $this->_prepareData($arr_XYZ_data);
          }
        }
        $form->handleRequest($request);              
        if (($form->isSubmitted())) {          
            $obj_XYZ->setXYZId($id);
            $str_hiddenfield_result = $form->get('extraformfield')->getData();
            $arr_hiddenfield_result = explode('&',$str_hiddenfield_result);     
            $obj_XYZ->setDef($obj_XYZ->getDef()->getDefId()); 
            $obj_XYZ->setAbc($arr_hiddenfield_result[3]); 
            $obj_XYZ->setAuthor(1); //ldap session value
            $em = $this->getDoctrine()->getManager();
            $em->persist($obj_XYZ);           
            $em->flush();                 

            $this->addFlash('success',  'Your record has been added successfully!');
            return $this->redirectToRoute('XYZ_index', array(), 301);
        }
    }

anyone can suggest me how can i achieve this ?

Upvotes: 0

Views: 330

Answers (2)

Ashish  Kumar Saxena
Ashish Kumar Saxena

Reputation: 4710

After a lot of R&D i got the solution:

use: 
 $em->merge($obj_XYZ) instead of  $em->persist($obj_XYZ); 

   before 
$em->flush();

Upvotes: 0

Hidde
Hidde

Reputation: 11961

Some remarks:

  • Are you calling the right URL (with $id != 0)?
  • Check if the form that is submitted is valid before doing anything.
  • Why are you calling setId() on the entity? Doctrine will set the ID of a new and persisted entity.

Finally, you are using a 301 redirect, which is a Permanent Redirect. This means that the browser will redirect any request to entity/entity/{id} to whichever URL is generated by XYZ_index.

I would recommend the following things:

  • Use isValid instead of isSubmitted for the form (it might not be valid but you are persisting its data!).
  • Use the built-in Forms, which you can load with an entity (so that you do not have to process data fields yourself).
  • Return a $this -> redirectToRoute('...', array(...)) instead of a 301.

Upvotes: 2

Related Questions