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.
<var name="type">file</var>
<var name="path">var/import</var>
<var name="filename"><![CDATA[stores.csv]]></var>
<var name="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.
<var name="delimiter"><![CDATA[,]]></var>
<var name="enclose"><![CDATA["]]></var>
<var name="fieldnames">true</var>
<var name="store"><![CDATA[0]]></var>
<var name="number_of_records">1</var>
<var name="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;
public function load() {
// you have to create this method, enforced by Mage_Dataflow_Model_Convert_Adapter_Interface
}
public function save() {
// you have to create this method, enforced by Mage_Dataflow_Model_Convert_Adapter_Interface
}
public function getStoreModel()
{
if (is_null($this->_storeModel)) {
$storeModel = Mage::getModel('baobaz_store/store');
$this->_storeModel = Mage::objects()->save($storeModel);
}
return Mage::objects()->load($this->_storeModel);
}
public function 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();
return true;
}
}
Now when you have this file created you can modify a little bit the declaration of parser adding adapter and method variables:
<var name="delimiter"><![CDATA[,]]></var>
<var name="enclose"><![CDATA["]]></var>
<var name="fieldnames">true</var>
<var name="store"><![CDATA[0]]></var>
<var name="number_of_records">1</var>
<var name="decimal_separator"><![CDATA[.]]></var>
<var name="adapter">baobaz_store/convert_adapter_store</var>
<var name="method">saveRow</var>
</action>
Having this done you should have your xml definition of custom dataflow profile looking like that:
<var name="type">file</var>
<var name="path">var/import</var>
<var name="filename"><![CDATA[stores.csv]]></var>
<var name="format"><![CDATA[csv]]></var>
</action>
<action type="dataflow/convert_parser_csv" method="parse">
<var name="delimiter"><![CDATA[,]]></var>
<var name="enclose"><![CDATA["]]></var>
<var name="fieldnames">true</var>
<var name="store"><![CDATA[0]]></var>
<var name="number_of_records">1</var>
<var name="decimal_separator"><![CDATA[.]]></var>
<var name="adapter">baobaz_store/convert_adapter_store</var>
<var name="method">saveRow</var>
</action>
You can now enjoy your custom dataflow 





