2010-09-24 60 views
3

基本上,我需要獲取我的客戶的CSV文件,每天在腳本中自動生成。 我嘗試了幾種方法,但它們太慢或實際上已經耗盡內存。如何循環Magento集合?

* 1)通過收集資源*

$collection = Mage::getResourceModel('customer/customer_collection') 
->addAttributeToSelect('email') 
->addAttributeToSelect('created_at') 
->joinAttribute('billing_company', 'customer_address/company', 'default_billing', null, 'left') 
->joinAttribute('billing_street', 'customer_address/street', 'default_billing', null, 'left') 
->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left') 
->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left') 
->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left') 
->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left') 
->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left'); 

foreach($collection as $customer) { 
echo $customer->getFirstname() . ","; 
} 

2)的foreach和負載客戶

$collection = Mage::getResourceModel('customer/customer_collection'); 
foreach($collection as $customer) { 
    $fullcustomer = Mage::getModel("customer/customer")->load($customer->getId()); 
    echo $fullcustomer->getFirstname() . ","; 
} 

任何想法的foreach?

謝謝!

+0

這兩項解決方案似乎可行,但雙方似乎特別優化。你能否用這兩種方法解決問題,以便我們可以告訴你在找什麼? – 2010-09-24 13:35:55

+0

嘗試在Magento中使用資源迭代器。看到這個職位:http://stackoverflow.com/questions/7419886/using-magentos-resource-iterator-model-on-a-product-collection – awaage 2012-09-15 02:27:06

回答

4

在每個Customer對象上使用load()將顯着減少減慢您的代碼。我建議你添加所需的屬性集合的第一種方法是正確的,但像完成它:

$collection->toArray([$arrRequiredFields = array()]); 

這樣一來,個人客戶不會被裝載,但toArray()doco here)將給你你想要的字段,然後你可以迭代多維數組來產生你的逗號分隔的字符串。

或者,您可以嘗試$collection->toXml()並使用XML,如果您對此感到滿意。 Google會將您指向xml-> csv轉換方法。

+0

恐怕我得到了空白頁面用盡了內存.. 。 – 2010-09-29 16:32:51

9

嘗試分頁大集合!

這個想法是,如果你可以加載更小的塊集合,你將不會使用盡可能多的內存。 加載一個塊(頁面),然後對其進行處理,如將其保存到文本文件中,然後加載下一個塊。結果是,你已經與整個較大的集合一起工作,但只會產生最大頁面的內存成本。

我們使用類似的東西來從我們的商店導出訂單。 我插入你的收藏,它似乎工作。

<?php 

if(php_sapi_name()!=="cli"){ 
echo "Must be run from the command line."; 
}; 

/** 
* Setup a magento instance so we can run this export from the command line. 
*/ 

require_once('app/Mage.php'); 
umask(0); 

if (!Mage::isInstalled()) { 
    echo "Application is not installed yet, please complete install wizard first."; 
    exit; 
} 

// Only for urls // Don't remove this 
$_SERVER['SCRIPT_NAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_NAME']); 
$_SERVER['SCRIPT_FILENAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_FILENAME']); 

Mage::app('admin')->setUseSessionInUrl(false); 
Mage::setIsDeveloperMode(true); ini_set('display_errors', 1); error_reporting(E_ALL); 

try { 
    Mage::getConfig()->init(); 
    Mage::app(); 
} catch (Exception $e) { 
    Mage::printException($e); 
} 
ini_set('memory_limit','500M'); 

$customerCount = 0; 
try{ 
    //configure the collection filters. 
    $collection = Mage::getResourceModel('customer/customer_collection') 
    ->addAttributeToSelect('email') 
    ->addAttributeToSelect('created_at') 
    ->joinAttribute('billing_company', 'customer_address/company', 'default_billing', null, 'left') 
    ->joinAttribute('billing_street', 'customer_address/street', 'default_billing', null, 'left') 
    ->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left') 
    ->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left') 
    ->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left') 
    ->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left') 
    ->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left'); 

    //Add a page size to the result set. 
    $collection->setPageSize(100); 
    //discover how many page the result will be. 
    $pages = $collection->getLastPageNumber(); 
    $currentPage = 1; 
    //This is the file to append the output to. 
    $fp = fopen('/tmp/customers.csv', 'w'); 
    do{ 
     //Tell the collection which page to load. 
     $collection->setCurPage($currentPage); 
     $collection->load(); 
     foreach ($collection as $customer){ 
      //write the collection array as a CSV. 
      $customerArray = $customer->toArray(); 
      //var_dump($customerArray); echo "\n\n"; 
      fputcsv($fp, $customerArray); 
      $customerCount++; 
     } 
     $currentPage++; 
     //make the collection unload the data in memory so it will pick up the next page when load() is called. 
     $collection->clear(); 
    } while ($currentPage <= $pages); 
    fclose($fp); 
} catch (Exception $e) { 
    //$response['error'] = $e->getMessage(); 
    Mage::printException($e); 
} 
echo "Saved $customerCount customers to csv file \n"; 
?> 

我救它作爲exportCustomers.php。

然後設置你的cron任務運行: PHP -f /path/to/your/magento/exportCustomers.php