2012-02-08 56 views
0

我想做一個cronjob出口一個收集到csv爲ciao和kelkoo比較價格。如何將收藏集導出到Magento的csv中?

我在哪裏可以看到Magento核心的一個例子?

我看到在未來的位置:

Mage/Dataflow/Model/Batch/export.php 

,但這個文件鴕鳥政策找到什麼。

也許可以看到如何在工作:

Mage/importexport/ 

回答

4

看一看Mage_Adminhtml_Block_Widget_Grid::getCsv()Mage_Adminhtml_Block_Widget_Grid::getCsvFile()作爲一個例子。
以下是第一個的相關部分作爲示例(由我添加的註釋)。

public function getCsv() 
{ 
    $csv = ''; 
    $this->_isExport = true; 
    $this->_prepareGrid(); // add the attributes to load, maybe required filters 
    $this->getCollection()->getSelect()->limit(); // only unique records 
    $this->getCollection()->setPageSize(0); // no paging, all records matching the set filters 
    $this->getCollection()->load(); 
    $this->_afterLoadCollection(); // load additional data on the collection items if needed 

    $data = array(); 
    // This foreach block adds headers to the columns 
    foreach ($this->_columns as $column) { 
     if (!$column->getIsSystem()) { 
      $data[] = '"'.$column->getExportHeader().'"'; 
     } 
    } 
    $csv.= implode(',', $data)."\n"; 

    // $column is an instance of Mage_Adminhtml_Block_Widget_Grid_Column 
    // Just a wrapper for getting the values from the collection items 
    foreach ($this->getCollection() as $item) { 
     $data = array(); 

     foreach ($this->_columns as $column) { 
      if (!$column->getIsSystem()) { 
       $data[] = '"' . str_replace(array('"', '\\'), array('""', '\\\\'), 
        $column->getRowFieldExport($item)) . '"'; 
      } 
     } 
     $csv.= implode(',', $data)."\n"; 
    } 

    // Grid totals are only used by reports 
    if ($this->getCountTotals()) 
    { 
     $data = array(); 
     foreach ($this->_columns as $column) { 
      if (!$column->getIsSystem()) { 
       $data[] = '"' . str_replace(array('"', '\\'), array('""', '\\\\'), 
        $column->getRowFieldExport($this->getTotals())) . '"'; 
      } 
     } 
     $csv.= implode(',', $data)."\n"; 
    } 

    return $csv; 
} 
+0

對我很有幫助。謝謝!! – davidselo 2012-02-08 14:44:56