简体   繁体   中英

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.

Calling getData() on a FileType will return an UploadedFile instance. This represents the file in your server after transfer. Then you have to do with it as you see fit: Move it to a folder if it is a simple upload or, as in your case, do some further processing.

You might decide to use a library instead of builtin functions, but hopefully this will get you on your way:

function upload(Request $request, EntityManager $em)
{
    // Form creation ommited
    $form2->handleRequest($request);
    
    if ($form2->isSubmitted() && $form2->isValid()) {
        /** @var UploadedFile */
        $file = $form2->get('submitFile')->getData();   
        
        // Open the file
        if (($handle = fopen($file->getPathname(), "r")) !== false) {
            // Read and process the lines. 
            // Skip the first line if the file includes a header
            while (($data = fgetcsv($handle)) !== false) {
                // Do the processing: Map line to entity, validate if needed
                $entity = new Entity();
                // Assign fields
                $entity->setField1($data[0]);
                $em->persist($entity);
            }
            fclose($handle);
            $em->flush();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM