PHP code example of le0m / yii2-import

1. Go to this page and download the library: Download le0m/yii2-import 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/ */

    

le0m / yii2-import example snippets


$importer = new Importer([
    // this can be an array of strings to use as column names for the imported data,
    // `true` if the column names can be extracted from the file itself (the 'how' is specific to the implementation)
    // or `false` to disable and not use column names (default).
    'columnNames' => [...],
    
    // see below for how to setup this
    'reader' => [
        'class' => '\common\components\importer\CsvReader',
    ],
    
    // see below for how to setup this
    'importStrategy' => [
        'class' => '\common\components\importer\ARImportStrategy',
    ]
]);

// get the imported data
$data = $importer->import();

'reader' => [
    'class' => '\common\components\importer\CsvReader',
    
    // limit the max length of a single line to read from file
    // 0 means no limit
    'lineLength' => 0,
    
    // character used to separate values on a single line
    'valueDelimiter' => ",",
    
    // character used to enclose values
    'valueEnclosure' => '"',
    
    // character used as escape in the values
    'escapeCharacter' => "\\",
]

'reader' => [
    'class' => '\common\components\importer\JsonReader',
]

'importStrategy' => [
    'class' => '\common\components\importer\ARImportStrategy',
    
    // name of the ActiveRecord class to use for the import
    'className' => '...',
    
    // if you need custom code to handle loading data read from the file to the AR
    // (ex. column names from the file are different than property names from the AR
    // this function will be called for each element in the original file,
    // return false to not import the current element ([[Importer]] ignores false elements)
    'loadProperties' => function ($model, $data) { ... },
    
    // whether to automatically save the AR at the end of the import
    // AR with validation errors are returned anyway
    'saveRecord' => true,
]