csvimport

Import from CSV into your models.
4 followers

CSV Import. Allows for either displaying imported data in form view, or just importing directly. There are some other good extensions out there, but none that could provide the ability to show in form, or directly import.

Requirements

  1. Yii 1.1 and above.
  2. Yiibooster: http://yiibooster.clevertech.biz/ (specifically, I use the alerts: http://yiibooster.clevertech.biz/widgets/decorations/view/alert.html)

Usage

This extension allows the user to import data from csv.

Steps to set up:

In protected/config/main.php, add the below in the modules array:

'import'=>array(
             'class'=>'ext.import.ImportModule',
             'onAfterImport' => array('ImportEvent', 'onAfterImport'),
             'onBeforeShowForm' => array('ImportEvent', 'onBeforeShowForm'),
        ),

Model Behavior. Add something like the below (with all your data) into your Model.

public function behaviors() {
    return array(
        'import' => array(
            'class' => 'ext.import.behaviors.ImportBehavior',
            //name of model
            'model'=>'Deals',
            //name of the controller
            'controller'=>'Deals',
            'fields'=>array(
                'title'=>array('displayName'=>'Title', 'sample'=>'Oranges'),
                'itemName'=>array('displayName'=>'Item Name', 'sample'=>'Oranges'),
                'price'=>array('displayName'=>'Price', 'sample'=>'4 for $1'),
                'description'=>array('displayName'=>'Description', 'sample'=>'really good oranges'),
            ),
            //url that the user is returned to after successful import
            'returnUrl'=> '/deals/index',
            //the "title" field of the model
            'titleField'=> 'title',
            //do you want the user to see the data in form view
            'showImportForm'=> false,
            //only used if "showImportForm" is set to true
            //the view that must exist in the model's view folder
            'importView'=>'show_import_stores_ext'
        ),
    );
}

Add actions to Controller. Make sure to include your correct Model name.

public function actions()
{
    return array(
        'import'=>array('class'=>'ext.import.components.ImportModels', 'model'=>'Deals'),
        'template'=>array('class'=>'ext.import.components.ImportTemplate', 'model'=>'Deals')
    );
}

Direct your user to the import url. In my example case, it's "deals/import".

If needed, you can add a method named onBeforeShowForm to your Model class. If you have this method, it will be called, allowing you to do some preprocessing of the data. It's called like this:

public function onBeforeShowForm($model)

This is called for each csv row, or $model, having been populated with the row of data in the csv.

Another "Event" is "afterImport". It's called after the import, and all imported model id's are passed to it, in array form. It's called like this:

public function afterImport($modelIds)

Resources

Github https://github.com/jamesmbowler/yii-csv-import

Be the first person to leave a comment

Please to leave your comment.

Create extension
Downloads
No downloadable files yet