Reputation: 2128
I am new to magento and I am trying to import a csv file from my custom module. I will try to explain it more. I have created a module to enter a region information(i.e region name, cost etc..), it works perfectly, I can add new region.Now i'm trying to add a "import" button from wihch i can import a csv file that contain the information about multiple region which is to be saved in my region table in magento's database.FYI I am using flat table.
Till now I am able to bring a button on the Grid view aside "Add New" while clicking, it redirects to a import function. after this I couldn't figure out what to do, I think the choose file menu should appear to choose my csv but i couldn't get to it.Can anyone help please.Any help would be appriciable.
Thanx in Advance
Upvotes: 2
Views: 4188
Reputation: 15206
I assume you have a resource model for your custom entity (otherwise it won't work).
The resource model should be placed in [Namespace]/[Module]/Model/Resource/[Entity].php
.
Create this method inside that resource model
public function saveImportData(array $data){
if (!empty($data)) {
$columns = array('region_name', 'cost'); //add here all your column names except the autoincrement column.
$this->_getWriteAdapter()->insertOnDuplicate($this->getMainTable(), $data, $columns);
}
return $this;
}
Now in the action where you upload your file, parse that file and create an array with the rows to be inserted.
Your array should look like this:
$toImport = array(
array('region_name' => 'Region 1', 'cost'=>'12.99', ...rest of the fields here),
array('region_name' => 'Region 2', 'cost'=>'17.99', ...rest of the fields here),
....
)
Now you just need to call in the controller
Mage::getResourceModel('[module]/[entity]')->saveImportData($toImport);
Upvotes: 1