2017 © Pedro Peláez
 

yii2-extension yii2-import

An extension for the Yii2 framework to import data from files

image

le0m/yii2-import

An extension for the Yii2 framework to import data from files

  • Friday, May 4, 2018
  • by le0m
  • Repository
  • 0 Watchers
  • 0 Stars
  • 3 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 50 % Grown

The README.md

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,
]

The Versions

04/05 2018

dev-master

9999999-dev

An extension for the Yii2 framework to import data from files

  Sources   Download

BSD-3-Clause

The Requires

 

by Leo Mainardi

csv json yii2 activerecord import

04/05 2018

v1.0

1.0.0.0

An extension for the Yii2 framework to import data from files

  Sources   Download

BSD-3-Clause

The Requires

 

by Leo Mainardi

csv json yii2 activerecord import