PHP code example of iamgold / dataloader-php

1. Go to this page and download the library: Download iamgold/dataloader-php 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/ */

    

iamgold / dataloader-php example snippets

=
composer 
=
// prepare column definitions
$defs = [
    ['id', 'int', 0], // name, type, defaultValue
    ['name', 'string', '-'],
    ['gender', 'string', '-'],
    ['email', 'email', '-'],
    ['join_date', 'dateTime', '-']
];

// prepare columns for each definitions and mapping related type calss.
$columns = [];
foreach($defs as $def) {
    list($name, $type, $defaultValue) = $def;
    $typeClass = match ($type) {
        'int' => new Integer,
        'string' => new Strings,
        'dateTime' => new DateTime,
        'email' => new Email,
        default => null,
    };

    if ($typeClass === null)
        die("The type($type) doest't support");

    // create column by name and type
    $columns[] = new Column($name, $typeClass);
}

// prepare data
$data = [
    [1, 'Eric', 'male', '[email protected]', date('Y-m-d')],
    [2, 'Peter', 'male', '[email protected]', date('Y-m-d')],
    [3, 'Marry', 'female', '[email protected]', date('Y-m-d')],
    [5, 'Jenny', 'female', '[email protected]', date('Y-m-d')],
    [100, 'Mandy', 'f', '[email protected]', date('Y-m-d')],
];

// create a file manager for storing file
$fileManager = new FileManager(__DIR__, 'csv', '20221113_');

// create loader for collecting data and parsing into columns
$loader = new CsvLoader($columns, ',', true);
$loader->setFileManager($fileManager);

// push data
foreach($data as $r) {
    $loader->push($r, false);
}

// load by file manager
$result = $loader->load();