views:

43

answers:

1

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!

A: 

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:

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

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.

HTH, JD

Jonathan Day
I'm afraid that I get the same blank page exhausted of memory...
Ignacio Pascual