PHP code example of kanryu / quick-csv

1. Go to this page and download the library: Download kanryu/quick-csv library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kanryu / quick-csv example snippets


use Kanryu\QuickCsv\QuickCsvImporter;

// -- (initialize begins)
$table_field_tmpl = array(
    array('name' => 'productId',   'type' => 'decimal(15)',   'maxlength' => 15,   ), 
    array('name' => 'categoryId',  'type' => 'decimal(9)',    'maxlength' => 9,    '' => 8,    'kCsvImporter([
    'destTableName' => 'Product', 
    'destPrimaryKey' => 'productId', 
    'fieldSchema' => $table_field_tmpl
]);
$qcsv->setPdo($pdo); // set your pdo or other RDB drivers;

$qcsv->create(); // create temporary table for importing
$qcsv->import('./test.csv'); // import csv to the temporary table
// -- (initialize ends)

// -- (validate begins)
$qcsv->validateAllFields(); // validate all records, all fields
$qcsv->validateDuplicatedId('productId'); // validate duplicated uniqued field

// If the specified field is the primary key of an external table, the key must exist in the external table.
$qcsv->validateNonExistForeignKey('categoryId', 'categoryId', 'Category', 'deleteFlag = 0');
// -- (validate ends)

// -- merge to the destination table
$qcsv->updateExistingRecords(); // Overwrite records existing in the destination table with CSV
$qcsv->insertNonExistingRecords(); // Add a new record from CSV that does not exist in the destination table