The flexibility of Magento Dataflow module lies in fact you can easily create your own adapters, parsers, mappers and apply them to your specific dataflow needs.
The basic case you may wonder how to do, is import of data for your custom module. Let's do this by example. Imagine you need to display on you e-shop list of stores, you have created custom module, table in database and datamodel part, all you need now is to populate this table with data you have within csv file.
Read the file
First you have to read the file. As you already know (if you read Magento Dataflow - Default Adapters [Part 2]) you can use dataflow/convert_adapter_io adapter for this.
<varname="type">file</var>
<varname="path">var/import</var>
<varname="filename"><![CDATA[stores.csv]]></var>
<varname="format"><![CDATA[csv]]></var>
</action>
Parse the file content
Now that you have read the file content, you should parse it using dataflow/convert_parser_csv.
<varname="delimiter"><![CDATA[,]]></var>
<varname="enclose"><![CDATA["]]></var>
<varname="fieldnames">true</var>
<varname="store"><![CDATA[0]]></var>
<varname="number_of_records">1</var>
<varname="decimal_separator"><![CDATA[.]]></var>
</action>
Process rows of data
Now the custom part of this process. Within your custom module you have to create custom adapter that will create row in database for each processed row of parsed file. Within your module root directory create file ./Model/Convert/Adapter/Store.php of this content:
extends Mage_Dataflow_Model_Convert_Adapter_Abstract
{
protected $_storeModel;
publicfunction load(){
// you have to create this method, enforced by Mage_Dataflow_Model_Convert_Adapter_Interface
}
publicfunction save(){
// you have to create this method, enforced by Mage_Dataflow_Model_Convert_Adapter_Interface
}
publicfunction getStoreModel()
{
if(is_null($this->_storeModel)){
$storeModel= Mage::getModel('baobaz_store/store');
$this->_storeModel = Mage::objects()->save($storeModel);
}
return Mage::objects()->load($this->_storeModel);
}
publicfunction saveRow(array$importData)
{
$store=$this->getStoreModel();
if(empty($importData['code'])){
$message= Mage::helper('catalog')->__('Skip import row, required field "%s" not defined','code');
Mage::throwException($message);
}
else
{
$store->load($importData['code'],'code');
}
$store->setCode($importData['code']);
$store->setName($importData['name']);
$store->save();
returntrue;
}
}
Now when you have this file created you can modify a little bit the declaration of parser adding adapter and method variables:
<varname="delimiter"><![CDATA[,]]></var>
<varname="enclose"><![CDATA["]]></var>
<varname="fieldnames">true</var>
<varname="store"><![CDATA[0]]></var>
<varname="number_of_records">1</var>
<varname="decimal_separator"><![CDATA[.]]></var>
<varname="adapter">baobaz_store/convert_adapter_store</var>
<varname="method">saveRow</var>
</action>
Having this done you should have your xml definition of custom dataflow profile looking like that:
<varname="type">file</var>
<varname="path">var/import</var>
<varname="filename"><![CDATA[stores.csv]]></var>
<varname="format"><![CDATA[csv]]></var>
</action>
<actiontype="dataflow/convert_parser_csv"method="parse">
<varname="delimiter"><![CDATA[,]]></var>
<varname="enclose"><![CDATA["]]></var>
<varname="fieldnames">true</var>
<varname="store"><![CDATA[0]]></var>
<varname="number_of_records">1</var>
<varname="decimal_separator"><![CDATA[.]]></var>
<varname="adapter">baobaz_store/convert_adapter_store</var>
<varname="method">saveRow</var>
</action>
You can now enjoy your custom dataflow