2

For a homepage I am getting data like this from my module's table:

Mage::getModel('my_module')->getCollection()->getData();

I wonder if this is the best thing performance wise. There are only 6 rows in that table. Would it be better to load it like this:

for($i = 1; $i < 7; $i++){
    Mage::getModel('my_module')->load($i);
}

Thanks!

3

I'd say none of them.

Depending on what you need to achieve I would use collection without loading model and selecting only the attributes you need.

Example: I want to retrieve only name and description of the products with ID: 1,2,3,4,5,6 and 7:

$collection = Mage::getResourceModel('catalog/product_collection')
                    ->addAttributeToSelect(array('name','description'))
                    ->addAttributeToFilter('entity_id',array('in'=>array(1,2,3,4,5,6,7)));

foreach ($collection as $product)
{
    echo $product->getName();
    echo $product->getDescription();
}

Always try to avoid loading entire object, especially in a loop.

2

Loading a full object like Mage::getModel('my_module')->load($i) takes a lot of time.

It is always a good idea to use the collection model Mage::getModel('my_module')->getCollection() instead of loading the full model for better performance.

Collection models always get data from a single query but every call of load() makes a new select query, so it's best to use getCollection().

Calling getData() at Mage::getModel('my_module')->getCollection(); is not a good idea. Use for loops for getting data.

And filter the collection by AddAttributeToFilter() (for EAV model) or AddFiledToFilter() (for flat table).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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