Symfony form to retrieve data from a CSV file and inject it into the database

I don’t understand how to do with a FileType form to retrieve data from csv and inject it into database.

With a normal form, I retrieve my data without problems.

//Controller.php

        $data = new Data();

        $form = $this->createFormBuilder($data)
                        ->add('name')
                        ->add('Nbmessage', IntegerType::class)
                        ->add('status')
                        ->add('details')
                        ->getForm();

        $form->handleRequest($request);

        if($form->isSubmitted() && $form->isValid())
        {
            $em->persist($data);
            $em->flush();
        }
---------------------------------------------------------------

// import.html.twig

    {{ form_start(form)}}


    {{ form_widget(form) }}
        <button type="submit" class="btn btn-success">Add</button>

    {{ form_end(form)}}

But I would like to get this data format located in a csv, with a Symfony Form.

Here is where I am for the moment, after many attempts

        $form2 = $this->createFormBuilder()
        ->add('submitFile', FileType::class)
        ->getForm();


        if ($request->getMethod('post') == 'POST') {

         $form2->handleRequest($request);


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

            $file = $form2->get('submitFile');


            $file->getData();
          }

        } 

I’ve been looking for a week … I really don’t understand how to do it.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

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