Doubt Goodby / csv component of the Packagist

0

I would like to know if anyone has used the component mentioned in the title (Goodby / csv) for PHP, because I have a problem with it. I explain, I am developing an application that manages software licenses where users are loaded by csv files to a MySQL database and for that I want to contemplate that every time I upload a file with the students, each record (csv row) is checked for see if it exists in the BD and do not duplicate it. The problem is that the component seems to be making an implicit loop of all the sentences that are delimited (csv) and in that interval I can not check each row for that purpose. I pass the code to see if you can see it clearly.

This is one of the functions that are responsible for what was commented:

public function InsertarProfesores($ficheroTmp){
        try{
            $config = new LexerConfig();
            $config
                ->setDelimiter(";") // Customize delimiter. Default value is comma(,)
                ->setEnclosure("'")  // Customize enclosure. Default value is double quotation(")
                ->setEscape("\")    // Customize escape character. Default value is backslash(\)
                ->setToCharset('UTF-8') // Customize target encoding. Default value is null, no converting.

            ;
            $lexer = new Lexer($config);
            $interpreter = new Interpreter();
            $pdo = $this->pdo;
            $interpreter->addObserver(function(array $columns) use ($pdo) {

                $stmt = $pdo->prepare('INSERT INTO profesores (departamento,dni,nombre, primer_apellido, segundo_apellido,tlf_sms,direccion,email,num_afiliacion, tutor_de) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
                $stmt->execute($columns);
            });

            $lexer->parse($ficheroTmp, $interpreter);

        }catch(Exception $e){
            die($e->getMessage());
        }
    }
    
asked by Mohicano 10.06.2017 в 23:33
source

1 answer

0

Good!

I have not used the Goodby / csv , but from what I see in the code, it makes a loop to "prepare" the query with the records, have you tried to include there a SELECT ? Although I do not know very well how you would recover the value of the DNI.

My advice is to use the native functions of PHP.

public function InsertarProfesores($ficheroTmp){
    try{
        $config = new LexerConfig();
        $config
            ->setDelimiter(";") // Customize delimiter. Default value is comma(,)
            ->setEnclosure("'")  // Customize enclosure. Default value is double quotation(")
            ->setEscape("\")    // Customize escape character. Default value is backslash(\)
            ->setToCharset('UTF-8') // Customize target encoding. Default value is null, no converting.

        ;
        $lexer = new Lexer($config);
        $interpreter = new Interpreter();
        $pdo = $this->pdo;
        $interpreter->addObserver(function(array $columns) use ($pdo) {
            $stmt = $pdo->prepare('SELECT dni FROM profesores WHERE departamento = ? AND dni = ?');
            $stmt->execute($columns);
            $cuenta = $stmt->rowCount();
            echo $cuenta ;
            if($cuenta){
                $stmt = $pdo->prepare('INSERT INTO profesores (departamento,dni,nombre, primer_apellido, segundo_apellido,tlf_sms,direccion,email,num_afiliacion, tutor_de) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
                $stmt->execute($columns);
            }
        });

        $lexer->parse($ficheroTmp, $interpreter);

    }catch(Exception $e){
        die($e->getMessage());
    }
}
    
answered by 12.06.2017 в 11:15