如何循环 Magento 集合?
基本上,我需要获取客户的 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() . ",";
}
有什么想法吗?
谢谢!
Basically, I need to get a CSV file of my customers, generated automatically in a script every day.
I've tried several ways, but they are too slow or actually exhausted of memory.
*1) foreach through collection resource *
$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 and load customer
$collection = Mage::getResourceModel('customer/customer_collection');
foreach($collection as $customer) {
$fullcustomer = Mage::getModel("customer/customer")->load($customer->getId());
echo $fullcustomer->getFirstname() . ",";
}
Any ideas?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试对大型集合进行分页!
这个想法是,如果您可以以较小的块加载集合,则不会使用那么多内存。
加载一个块(页面),然后对其执行某些操作,例如将其保存到文本文件中,然后加载下一个块。结果是,您使用了整个较大的集合,但只产生了最大页面的内存成本。
我们使用与此类似的方法从我们的商店导出订单。
我插入了你的收藏,它似乎有效。
我将其保存为exportCustomers.php。
然后设置您的 cron 任务来运行:
php -f /path/to/your/magento/exportCustomers.php
Try paging a large collection!
The idea is if you can load the collection in smaller chunks, you won't use as much memory.
Load a chunk(page), then do something with it like saving it out to a text file and then load the next chunk. The result being, you've worked with the whole larger collection, but only incurred the memory cost of the largest page.
We use something similar to this to export orders from our store.
I plugged in your collection and it seems to work.
I saved it as exportCustomers.php.
Then setup your cron task to run:
php -f /path/to/your/magento/exportCustomers.php
在每个 Customer 对象上使用
load()
会显着减慢您的代码速度。我建议您将所需属性添加到集合中的第一种方法是正确的方法,但可以用以下方法完成:这样,永远不会加载单个客户,而是
toArray()
(< a href="http://docs.magentocommerce.com/Varien/Varien_Data/Varien_Data_Collection.html#toArray" rel="nofollow noreferrer">doco here)将为您提供您想要的字段,然后您可以迭代在多维数组上生成逗号分隔的字符串。或者,您可以尝试
$collection->toXml()
并使用 XML(如果您对此感到满意)。 Google 会向您指出 xml->csv 转换方法。Using a
load()
on each Customer object will significantly slow down your code. I would suggest that your first approach of adding the required attributes to the collection is the right one, but finish it with something like:That way, the individual customers are never loaded, but
toArray()
(doco here) will give you the fields you want, then you can iterate over the multi-dimensional array to produce your comma-separated string.Alternatively, you could try
$collection->toXml()
and work with XML if you're comfortable with that. The Google will point you to xml->csv conversion methods.