GithubHelp home page GithubHelp logo

yii-csv-import's Introduction

This extension allows the user to import data from csv.

Steps to set up:

1. Add module to protected/config/main.php, in modules. Like this:

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

2. 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
            //must appear how it appears in url
            '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'
        ),
    );
}

3. 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')
    );
}

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

5. 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)

yii-csv-import's People

Contributors

jamesmbowler avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

yii-csv-import's Issues

ImportFile.php still getting issues

I saw your previous post about posting in the modules in the main.php file in config. I did so and I am still getting the error. include(ImportFile.php): failed to open stream: No such file or directory. I added the code as follows:

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

            ),

What could I be doing wrong?

error in loading ImportFile.php

First of all, thanks for your excellent contribution. It is exactly what I was looking for an planning to hardcode myself for a couple of models. This saves a lot of time and it set up in a very clean way.

I have followed the steps, but I get throw an error after running your extension.
Error: "include(ImportFile.php): failed to open stream: No such file or directory"
It happends in line 23 of the file /extensions/import/components/ImportModels.php(23).

Did I maybe misread the steps?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.