import data from Excel file through phpspreadsheet and check existence of records in the database symfony 4

 I would to import data from Excel file through phpspreadsheet and read row by row and test if the record exist in the database as follow :
                $reader = new XlsxReader();
                $spreadsheet = $reader->load($tmp_name);
                $worksheet = $spreadsheet->getActiveSheet();
                $highestRow = $worksheet->getHighestRow(); // e.g. 10
                $highestColumn = $worksheet->getHighestColumn();
                fwrite($fp, "File: " . $_FILES['catalogue']['name'] . "n");
                fwrite($fp, "Number of Row : " . $highestRow . "n");
                fwrite($fp, "Highest Column : " . $highestColumn . "n");
                $em = $this->getDoctrine()->getManager()->getRepository(Catalogue::class);
                $list = [];

                $it = $worksheet->getRowIterator(1);
                foreach ($it as $row) {
                    $cellIt = $row->getCellIterator();
                    $r = [];
                    foreach ($cellIt as $cell) {
                        $r[] = $cell->getFormattedValue();
                    }
                    $list[] = $r;
                }
                foreach ($list as $x) {
                    $v = $x[0];
                    fwrite($fp, $v . "n");
                    $s = $em->findOneBy(['Species' => $x[0]]);
                    if ($s) {
                        if ($s->getSpecies() ==$x[0]){
                        $request->getSession()->getFlashBag()->add('error', $v . 'Already Existing' . '<br>');
                        fwrite($fp, $x[0].'Already Exist in the catalogue'. "n");
                        }
                        if ($s->getSpecies() !=$x[0]) {
                        $request->getSession()->getFlashBag()->add('sucess', $x[0] . 'To be added' . '<br>');
                       fwrite($fp, $x[0]. 'to be added to the catalogue'. "n");
                        }

                    }

                    //
                }
fclose($fp);
                $request->getSession()->getFlashBag()->add('success', 'File is valid, and was successfully processed.!' . '<br>' . "<a href=" . $url . ">Log Link</a>");
                return $this->redirect($this->generateUrl('Catalogue_list'));

first the the check if the record exist in the database woks for the half of the records as follow 
 - Asparagopsis armata        
 - Abudefduf sexfasciatus
 - Abudefduf sexfasciatus Already Exist in the catalogue    
 - Abudefduf vaigiensis
 - Abudefduf vaigiensis Already Exist in the catalogue
 - Caretta caretta
 - Chelonya mydas
only the three first records are in the database. What im doing wrong thanks for the help

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

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