Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to insert record in Symfony?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.58k
    Comment on it

    Hello Friends,

    If you are new in symfony and want to insert a new record in the database, please review the below example :: 

    1) Please open your controller file.

    2) Add database entity in the top of your controller like this

    use PersonalInformationSystemBundle\Entity\PisUserIncrement;
    
    // PersonalInformationSystemBundle is my bundle name please replace this with your bundle name
    // PisUserIncrement is my database entity please replace this with your database entity
    

    3) An example of controller code for insertion as given below::

     public function addAction(Request $request)
        {
           $em = $this->getDoctrine()->getManager();
           // Get userid currently login
           $user      =   $this->get('security.token_storage')->getToken()->getUser();
           $userId    =   $user->getId();
    
           if($request->getMethod() == 'POST') { 
                $params             =     $request->request->all();
    
                // Define object for database entity
                $userIncrement      =     new PisUserIncrement();
    
                $now       =   new\DateTime('now');
                $params    =    $request->request->all();
    
                // Set variable through getter/setter as define in entity
                $userIncrement->setEmployeeId($params['userid']);
                $userIncrement->setIncrement($params['increment']);
                $userIncrement->setIncrementProvider( $userId);
                $userIncrement->setIncrementYear(date('Y'));         
                $userIncrement->setCreated($now);
                $userIncrement->setUpdated($now);
    
                $em = $this->getDoctrine()->getManager();
                $em->persist($userIncrement);
                $em->flush();
                $id  = $userIncrement->getId();
    
                // Set success message and redirect to listing page 
                $request->getSession()
                ->getFlashBag()
                ->add('success', 'User Incremented  succesfully!');
                return $this->redirectToRoute('pis_setuserincrement_list', array('month' => $params['month']));
            }
          }
    
    
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: