[Magento] How export and import are implemented?

本文深入探讨了电商世界中数据转换的核心概念与实践,包括加载数据、映射、格式化和保存过程,以及如何通过自定义组件实现数据从一种资源到另一种资源的有效转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Introduction

 

This long anticipated feature is arguably one of themost importantones in ecommerce world. Flexibility of this interface definescapability of the application to communicate with the outsideworld, be it legacy systems, 3rd party applications, priceaggregators, external order sources, fulfillment systems,accounting, etc.

Thus we took it very seriously and, as with everything elsein Magento wentone or more steps further than most systems. Effectivelyany import orexport is a conversion.From one medium to another. From database to local file,from FTP serverto database, from parsed HTML pageto Excel spreadsheet, from Google Spreadsheet to catalog.

Basically, it boils down to loading data from one resource,applying unlimited alterations/reformatting, and saving it toanother resource.

Welcome to the world of conversion.

Concept

 

Let’s take an example of exporting a product catalog to excelspreadsheet file. The logical steps are:

  • Load records from database
  • Convert internal identificators into human understandabletext
  • Map database fields to our custom file headings (sku ⇒ PartNumber)
  • Format data array as Excel XML
  • Save resulting text as local or remote file

This sequence of actions we call a Profile.

Here we have 4 categories of components:

  • Adapter (input/output)
  • Mapper (data value processing)
  • Parser (data format conversion)
  • Validator (data validation)

Adapters

Adapters are anything that can plug in to external resource. Thismay include:

  • Local files
  • FTP, SFTP servers
  • Database table
  • Soap services
  • Standard input/output
  • HTTP interface
  • HTML source
  • Google Spreadsheets
  • Custom resources (specialized db interface, cache repository,etc.)

Adapters can have 2 methods:

  • load - extract datafrom resource into internal container.
  • save - save data intoresource.

Mappers

Mappers are for processing data without changing it’s format orstructure. They’re useful for tasks such as mapping one field toanother or apply formulas on values.

Mappers can run one method: map

Parsers

Parsers are for changing format of data. Examples:

  • CSV text ⇒ Grid array
  • Excel XML text⇒ Grid array
  • Grid array ⇒ Product model collection

Parsers can have 2 methods:

  • parse - usually convertfrom more human readable data to machine friendly format
  • unparse - the opposite,into human readable format

You might notice that Parsers are separated from Adapters, whenusually they are associated as one thing (export to CSV file).

Validators

Validators are created to make sure that conditions are met. Theconditions can be applied to processed data or environmentvariables.

Validators can only validate.

Implementation

 

Let’s take an example of exporting catalog products into CSVfile.

  1. <action type="catalog/convert_adapter_product" method="load">
  2.     <var name="store">base</var>
  3. </action>
  4.  
  5. <action type="catalog/convert_parser_product" method="unparse">
  6. </action>
  7.  
  8. <action type="varien/convert_mapper_column" method="map">
  9.     <var name="sku">Part#</var>
  10. </action>
  11.  
  12. <action type="core/convert_parser_csv" method="unparse">
  13.     <var name="delimiter">,</var>
  14.     <var name="enclose"><![CDATA["]]></var>
  15.     <var name="fieldnames">true</var>
  16. </action>
  17.  
  18. <action type="core/convert_adapter_io" method="save">
  19.     <var name="type">file</var>
  20.     <var name="path"></var>
  21.     <var name="filename">products.csv</var>
  22.     <var name="link">/dev/moshe/magento/var/export/products.csv</var>
  23. </action>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值