jamesmbowler / yii-csv-import Goto Github PK
View Code? Open in Web Editor NEWAllow Importing into Models from CSV.
Allow Importing into Models from CSV.
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)
Hi,
First, thanks for your extension.
I'm using your extension in a Yii module. When I try to upload a csv file, it takes me to http://mysite.com/import/import/upload and show an error as above. Deals is a model in my module.
Any suggestion to fix this will be great. Thanks again.
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?
Somehow, only the first row gets imported and the rest is ignored. Any idea how that is possible?
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?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.