"Magento DataFlow - Data Exchange Made Flexible" article introduced global concept of data exchange framework implemented in Magento. Today I would like to tell more about default adapters implemented in DataFlow module.
-
Adapter definition
Adapters are responsible for pluging into an external data resource and fetching requested data or saving given data into data resource. For this purpose all adapters implement interface Mage_Dataflow_Model_Convert_Adapter_Interface which contains two methods: load() and save(). Data exchange concept introduced in DataFlow module use adapters in 3 contexts:
- to load data from resource - using load() method
- to save data to resource - using save() method
- to process one parsed row - when defined as adapter/method pair of variables of parser
For first two contexts adapter's xml definition looks like that:
<action type="dataflow/convert_adapter_io" method="load">
...
</action>Action tag has two parameters: type and method. Type tells as which adapter class is to be used in this action. It is defined using its alias. Method tells us which method of this adapter class action should call. As mentioned before, by default there are two available methods: load and save. Children of action tag define variables which are parameters used when executing adapter's method. Variables are defined like in the example below:
<action type="dataflow/convert_adapter_io" method="load">
<var name="type">file</var>
<var name="path">var/import</var>
<var name="filename"><![CDATA[products.csv]]></var>
<var name="format"><![CDATA[csv]]></var>
</action> -
Magento DataFlow default adapters
Magento DataFlow module contains few default adapter classes which you can find in app/code/core/Dataflow/Model/Convert/Adapter. Not all of them have yet implemented load() and save() methods.
For common case of reading data from or saving data to local or remote file you will use dataflow/convert_adapter_io (Mage_Dataflow_Model_Convert_Adapter_Io).
Following variables will allow you to define local/remote file as data source:
- type - defines type of io source we want to process. Valid values: file, ftp
- path - defines relative path to the file
- filename - defines data source file's name
- host - for ftp type it defines the ftp host
- port - for ftp type it defines the ftp port; if not given, default value is 21
- user - for ftp type it defines the ftp user, if not given default value is 'anonymous' and password then is 'anonymous@noserver.com'
- password - for ftp type it defines the ftp user's password
- timeout - for ftp type it defines connection timeout; default value is 90
- file_mode - for ftp type it defines file mode; default value is FTP_BINARY
- ssl - for ftp type if it is not empty, then ftp ssl connection is used
- passive - for ftp type it defines connection mode; default value is false
-
Customer and Product adapters
For most commonly exchanged entities - customer and product - Magento provides default adapters: customer/convert_adapter_customer (Mage_Customer_Model_Convert_Adapter_Customer) and catalog/convert_adapter_product (Mage_Catalog_Model_Convert_Adapter_Product). Both inherit from Mage_Eav_Model_Convert_Adapter_Entity.
To simply load all customers data for selected store you can use the following xml:
<action type="customer/convert_adapter_customer" method="load">
<var name="store">default</var>
</action>Sometimes you may want to not load all customers in database. To help you with this there are following variables valid:
- filter/firstname - to load only customers with firstname starting with value of this variable
- filter/lastname - to load only customers with lastname starting with value of this variable
- filter/email - to load only customers with email starting with value of this variable
- filter/group - to load only customers from group with id equal to value of this variable
- filter/adressType - to export only selected addressType; valid values are: both, default_billing, default_shipping
- filter/telephone - to load only customers with telephone starting with value of this variable
- filter/postcode - to load only customers with postcode starting with value of this variable
- filter/country - to load only customers with country iso code equal to value of this variable
- filter/region - to load only customers with region equal to value of this variable (for US just 2-letter state names)
- filter/created_at/from - to load only customers created after a date defined as value of this variable
- filter/created_at/to - to load only customers created before a date defined as value of this variable
For example:
<action type="customer/convert_adapter_customer" method="load">
<var name="store"><![CDATA[0]]></var>
<var name="filter/firstname"><![CDATA[a]]></var>
<var name="filter/lastname"><![CDATA[a]]></var>
<var name="filter/email"><![CDATA[a]]></var>
<var name="filter/group"><![CDATA[1]]></var>
<var name="filter/adressType"><![CDATA[default_billing]]></var>
<var name="filter/telephone"><![CDATA[1]]></var>
<var name="filter/postcode"><![CDATA[7]]></var>
<var name="filter/country"><![CDATA[BS]]></var>
<var name="filter/region"><![CDATA[WA]]></var>
<var name="filter/created_at/from"><![CDATA[09/22/09]]></var>
<var name="filter/created_at/to"><![CDATA[09/24/09]]></var>
</action>Same way you can load and filter products loaded from database with following variables:
- filter/name - to load only products with name starting with value of this variable
- filter/sku - to load only products with sku starting with value of this variable
- filter/type - to load only products with type defined as value of this variable; valid values are: simple, configurable, grouped, bundle, virtual, downloadable
- filter/attribute_set - to load only products with attribute set id equal to value of this variable
- filter/price/from - to load only products with price starting from value of this variable
- filter/price/to - to load only products with price up to value of this variable
- filter/qty/from - to load only products with quantity starting from value of this variable
- filter/qty/to - to load only products with quantity up to value of this variable
- filter/visibility - to load only products with visibility id equal to value of this variable
- filter/status - to load only products with status id equal to value of this variable
Example:
<action type="catalog/convert_adapter_product" method="load">
<var name="store"><![CDATA[0]]></var>
<var name="filter/name"><![CDATA[a]]></var>
<var name="filter/sku"><![CDATA[1]]></var>
<var name="filter/type"><![CDATA[simple]]></var>
<var name="filter/attribute_set"><![CDATA[29]]></var>
<var name="filter/price/from"><![CDATA[1]]></var>
<var name="filter/price/to"><![CDATA[2]]></var>
<var name="filter/qty/from"><![CDATA[1]]></var>
<var name="filter/qty/to"><![CDATA[2]]></var>
<var name="filter/visibility"><![CDATA[2]]></var>
<var name="filter/status"><![CDATA[1]]></var>
</action>
Seems a little bit frightening if you see all those id values you have to provide for filters. Fortunatelly for these two entities - customers and products - there is wizard like profile generator that allows you to define filter with simple select boxes.
In next part I will describe use of parsers and adapters in context of parsers.





