0

Code sample # 1

$customer_obj = Mage::getModel('customer/customer');
foreach(){
$customer_data = $customer_obj->load(customer_id);
$customer_data->getFirstname();
$customer_data->getEmail();
........
}

Code sample # 2

$customer_obj = Mage::getModel('customer/customer');
foreach(){
$customer_obj->load(customer_id)->getFirstname();
$customer_obj->load(customer_id)->getEmail();
.........
}

The loop count will be something around 1800-2000 every day.It is one time cron job every day

I am assuming it in this way, in sample # 1 $customer_data will have the entire customer details in memory and from there on wards it will fetch it from memory.but in sample # 2 it always hit db with every load call

Can someone please enlighten me?

2 Answers 2

0

For your question:

code sample 1 is better, because it only does the load once for each customer. code sample 2 does multiple loads per customer.

But as Patrick Steenks mentions, it is even much better to use a customer collection to do this, because then you only have ONE query for ALL customers :)

3
  • Thanks for that. But my customer base is almost 160,000. is it good to load that much of data in order to get the detail of <2000 customers?
    – zamil
    Commented Jul 5, 2014 at 12:10
  • @zamil You can Filter the collection to the IDs you want by using ->addAttributeToFilter('entity_id', array('in' => array(1,2,3,4))) this Would get customers with IDs 1, 2, 3 and 4
    – mpaepper
    Commented Jul 5, 2014 at 18:02
  • Or you can filter with specific conditions. Do a google search on which attribute you want to filter and you will get enough results. Commented Jul 6, 2014 at 7:27
3

I think it is better to use the collection method of magento, and then only load the attributes you need:

$collection = Mage::getModel("customer/customer")->getCollection()->addAttributeToSelect('firstname','email');

Then loop with a foreach through this collection.

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.