PHP code example of popov / php-importer

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

    

popov / php-importer example snippets


use Popov\Importer\Factory\DriverCreator;
use Popov\Importer\Importer;
use Popov\Db\Db;

$config = [
    'tasks' => [
        'discount-card' => [
            'driver' => 'libxl',
            'fields' => [
                [
                    // mapping fields in file to db fields with apply filters
                    'Nominal' => ['name' => 'discount', '__filter' => ['percentToInt']],
                    'Serial' => 'serial',
                    
                    // table where save imported data
                    '__table' => 'discount_card',
                    // shortcut name
                    '__codename' => 'discount',
                    // unique field name for avoid duplicate
                    '__identifier' => 'serial'
                ],
            ],
        ],
    ],
];

$pdo = new PDO('mysql:host=myhost;dbname=mydb', 'login', 'password'); 
$db = (new Db())->setPdo($pdo);

$factory = new DriverCreator($config);
$importer = new Importer($factory, $db);

if ($importer->import('discount-card', '/path/to/file.xls')) {
  echo 'Success import!';
} else {
  var_dump($importer->getErrors());
}

['driver' => 'Excel']

// from   =>   to
['Serial' => 'serial']

[
    'Nominal' => ['name' => 'discount', '__filter' => ['trim', 'percentToInt']]
]

'__table' => 'table_name',

'__codename' => 'discount',

'__identifier' => 'serial',
// or
'__identifier' => ['asin', 'marketplace'],

'__ignore' => ['comment'],

'__exclude' => false,

'__foreign' => ['customer_table' => 'customerId'],


'__options' => [
    'mode' => 'save'
]