Yii2 Importer
This extension helps you import data from files like CSV or JSON into your application., (*1)
Installation
The preferred way to install this extension is through composer., (*2)
Either run, (*3)
composer require le0m/yii2-import
or add, (*4)
"le0m/yii2-import": "*"
to the require section of your composer.json., (*5)
Usage
This extension provide support to import data from files to data structures like array or ActiveRecord., (*6)
First you must create an Importer
, which needs a reader
and an importStrategy
to work, then you can import:, (*7)
$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();
The importer will read the file using the chosen reader
implementation, passing each element to the chosen importStrategy
implementation. The returning value is an array of imported data., (*8)
If the importStrategy
return false
on a single element, the Importer
will ignore it., (*9)
By default this extension provides two reader (CSV and JSON) and two import strategy (to array and to ActiveRecord) implementations to choose from, but you can add your own by implementing the appropriate interface., (*10)
Import a CSV file, (*11)
The CsvReader
accepts the following properties:, (*12)
'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' => "\\",
]
Import a JSON, (*13)
The JsonImporter
has no particular properties:, (*14)
'reader' => [
'class' => '\common\components\importer\JsonReader',
]
Import to ActiveRecord, (*15)
The ARImportStrategy
accepts the following properties:, (*16)
'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,
]