gpt4 book ai didi

collections - 如何循环 Magento 集合?

转载 作者:行者123 更新时间:2023-12-03 18:27:40 24 4
gpt4 key购买 nike

基本上,我需要获取客户的 CSV 文件,每天在脚本中自动生成。
我尝试了几种方法,但它们太慢或实际上耗尽了内存。

*1) foreach 通过集合资源 *

$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() . ",";
}

有任何想法吗?

谢谢!

最佳答案

尝试对大型集合进行分页!

这个想法是,如果您可以以较小的块加载集合,则不会使用那么多内存。
加载一个块(页面),然后用它做一些事情,比如将它保存到一个文本文件中,然后加载下一个块。结果是,您已经处理了整个更大的集合,但只产生了最大页面的内存成本。

我们使用类似的东西从我们的商店导出订单。
我插入了你的收藏,它似乎有效。

<?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

关于collections - 如何循环 Magento 集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3786826/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com