PHPackages                             elfuvo/yii2-import-wizard - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [PDF &amp; Document Generation](/categories/documents)
4. /
5. elfuvo/yii2-import-wizard

ActiveYii2-extension[PDF &amp; Document Generation](/categories/documents)

elfuvo/yii2-import-wizard
=========================

Step-by-step import from Excel files with mapping of model attributes

0.3.1(4y ago)54.5k2[1 issues](https://github.com/eLFuvo/yii2-import-wizard/issues)MITPHPPHP &gt;=7.1.0

Since Aug 15Pushed 4y ago1 watchersCompare

[ Source](https://github.com/eLFuvo/yii2-import-wizard)[ Packagist](https://packagist.org/packages/elfuvo/yii2-import-wizard)[ RSS](/packages/elfuvo-yii2-import-wizard/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (7)Versions (14)Used By (0)

[![Latest Stable Version](https://camo.githubusercontent.com/d814fb7deaffdb4af740537671dc99c1a7b1371748b9b28e18f741b2695e9abb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f656c6675766f2f796969322d696d706f72742d77697a6172642e737667)](https://packagist.org/packages/elfuvo/yii2-import-wizard)[![Build](https://camo.githubusercontent.com/06e80bda6408dba3ff6942f91940329dd01e3045aff23071e805bde0b0929fa9/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f656c6675766f2f796969322d696d706f72742d77697a6172642f4275696c642e737667)](https://github.com/elfuvo/yii2-import-wizard)[![License](https://camo.githubusercontent.com/c1e99bb4c671504d513ea638bf88fedf0b914669647c0925cb61ffe5cdee093d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f656c6675766f2f796969322d696d706f72742d77697a6172642e737667)](https://github.com/elfuvo/yii2-import-wizard/blob/master/LICENSE)[![Yii2](https://camo.githubusercontent.com/d6b0929173e28cc627430d2519ca1853466a70f37395877eaf4820cb3e1e1909/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f506f77657265645f62792d5969695f4672616d65776f726b2d677265656e2e7376673f7374796c653d666c6174)](http://www.yiiframework.com/)

##### Table of Contents

[](#table-of-contents)

[Requirements](#requirements)
[Installation](#installation)
[Configure](#configure)

- [Module common configuration](#configure)
- [I18n messages](#i18n)
- [Actions](#actions)
    - [Upload import file action](#upload-import-file-action)
    - [Upload import file action with pre-defined attribute map](#upload-import-file-action-with-pre-defined-attribute-map)
    - [Import setup action](#import-setup-action)
    - [Current progress action](#current-progress-action)

[Queue job](#queue-job)
[Import link button](#import-link-button)
[Additional notes](#additional-notes)
[Screenshots](#screenshots)

Requirements
------------

[](#requirements)

- PHP &gt;=7.1

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist elfuvo/yii2-import-wizard "~0.2.0"

```

or add

```
"elfuvo/yii2-import-wizard": "~0.2.0"

```

to the "require" section of your `composer.json` file.

Configure
---------

[](#configure)

Configure service for importing, Importing result keeper and available import adapters.

```
// in common app config
[
'container'=>[
    'definitions' => [
           \elfuvo\import\services\ImportServiceInterface::class => [
                'class' => \elfuvo\import\services\ImportService::class,
                'casters' => [
                    // there is a custom value casters
                    // \elfuvo\import\services\BracketValueCaster::class,
                ]
            ],
            \elfuvo\import\result\ResultImportInterface::class =>
                \elfuvo\import\result\FileContinuesResultImport::class,
            \elfuvo\import\adapter\AdapterFabricInterface::class => [
                'class' => \elfuvo\import\adapter\AdapterFabricDefault::class,
                'adapters' => [
                    \elfuvo\import\adapter\AdapterImportExcel::class,
                    \elfuvo\import\adapter\AdapterImportCsv::class,
                ]
            ],
        ],
    ],
];
```

I18n
----

[](#i18n)

Add translations to i18n yii component:

```
[
    'components' => [
        'i18n' => [
                'class' => \yii\i18n\I18N::class,
                'translations' => [
                    'import-wizard' => [
                        'class' => \yii\i18n\PhpMessageSource::class,
                        'sourceLanguage' => 'en',
                        'basePath' => '@vendor/elfuvo/yii2-import/src/messages',
                    ],
                ],
        ]
    ]
];
```

Actions
-------

[](#actions)

Add import steps actions to your controller

```
class DefaultController extend \yii\web\Controller{

    public function actions()
    {
        return [
            'upload-file-import' => [ // action with form for uploading import file
                'class' => \elfuvo\import\actions\UploadFileAction::class,
                ...
            ],
            'setup-import' => [ // action with import configuration form
                'class' => \elfuvo\import\actions\SetupAction::class,
                ...
            ],
            'progress' => [ // action for showing current import progress/statistic and errors after import is done
                'class' => \elfuvo\import\actions\ProgressAction::class,
            ],
        ];
    }
}
```

### Upload import file action

[](#upload-import-file-action)

```
    public function actions()
    {
        return [
            'upload-file-import' => [
                'class' => \elfuvo\import\actions\UploadFileAction::class,
                'model' => new Review(), // model instance for import
                'nextAction' => 'setup-import', // next action name
                'progressAction' => 'progress', // name of progress action
            ],
            ...
        ];
    }
```

### Upload import file action with pre-defined attribute map

[](#upload-import-file-action-with-pre-defined-attribute-map)

If you are using attribute map, that you don't need configure setup action. Import starts after file uploaded.

```
    public function actions()
    {
        return [
            'upload-file-import-map' => [
                'class' => UploadFileAction::class,
                // model instance with pre-defined attribute values. It will be cloned in import service.
                // can be callable function that returns model
                'model' => new Review([
                    'language' => 'ru',
                    'hidden' => Review::HIDDEN_NO,
                ]),
                'startRowIndex' => 2,
                // can be callable function that return array of MapAttribute models
                // first argument for that function is first row from import file
                'attributeMap' => [
                    new MapAttribute([
                        'column' => 'A',
                        'attribute' => 'b24StationId',
                        'castTo' => BracketValueCaster::class,
                    ]),
                    new MapAttribute([
                        'column' => 'B',
                        'attribute' => 'title',
                        'castTo' => MapAttribute::TYPE_STRING,
                        'identity' => 1,
                    ]),
                    new MapAttribute([
                        'column' => 'C',
                        'attribute' => 'author',
                        'castTo' => MapAttribute::TYPE_STRING,
                    ]),
                    new MapAttribute([
                        'column' => 'D',
                        'attribute' => 'text',
                        'castTo' => MapAttribute::TYPE_STRING,
                    ]),
                    new MapAttribute([
                        'column' => 'E',
                        'attribute' => 'rating',
                        'castTo' => MapAttribute::TYPE_FLOAT,
                    ]),
                    new MapAttribute([
                        'column' => 'F',
                        'attribute' => 'publishAt',
                        'castTo' => MapAttribute::TYPE_DATETIME,
                    ]),
                ],
                'progressAction' => 'progress',
            ],
        ];
   }
```

### Import setup action

[](#import-setup-action)

```
    public function actions()
    {
        return [
        ...
        'setup-import' => [
                'class' => \elfuvo\import\actions\SetupAction::class,
                // model instance with pre-defined attribute values. It will be cloned in import service.
                /*'model' => new Review([
                      'hidden' => Review::HIDDEN_NO,
                      'language' => Yii::$app->language,
                      'createdBy' => Yii::$app->user->getId(),
                ])*/
                // can be callable function
                'model' => function(){
                    $importModel = new Review([
                        'hidden' => Review::HIDDEN_NO,
                        'language' => Yii::$app->language,
                        'createdBy' => Yii::$app->user->getId(),
                    ]);
                    // some behaviors do not work in console app (for queued import)
                    // there we can disable them
                    $importModel->detachBehavior('LanguageBehavior');
                    $importModel->detachBehavior('CreatedByBehavior');

                    return $importModel;
                },
                'scenario' => Review::SCENARIO_DEFAULT, // scenario of model validation when saving model from import
                'previousAction' => 'upload-file-import', // previous action name
                'excludeAttributes' => [ // exclude model attributes for building import map
                    'id',
                    'language',
                    'createdBy',
                    'createdAt',
                    'updatedAt'
                ]
            ],
        ];
    )
```

### Current progress action

[](#current-progress-action)

```
    public function actions() {
        return [
            ...
            'progress' => [
                'class' => \elfuvo\import\actions\ProgressAction::class,
                'model' => new Review(),
            ],
        ];
    }
```

Queue job
---------

[](#queue-job)

If queue component is not configured for Yii application then import will start immediately. Otherwise `ImportJob` will be added to the task queue.

Yii2 [queue](https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/README.md).

Import link button
------------------

[](#import-link-button)

Add import link button into your module view:

```
