I've encountered a problem where my foreach loop is causing a 504 Gateway timeout error so I did some research and found that for large collections it is better to use the core/resource_iterator
and the walk()
function.
This has definitely improved performance but I have lost the ability to return the array the I need from when I call my custom function.
$this->getAllDataFromTable()
Here is what I mean: This function will get all the data from the table and return the $array
variable which I can do stuff with - but is very expensive and times out with a 504 error.
public static function getAllDataFromTable()
{
$model = Mage::getModel('custom/table');
$collection = $model->getCollection();
$array = array();
foreach ($collection as $item) {
$array[] = $item->getData();
}
return $array;
}
Using the iterator: The iterator is more efficient but I can only do stuff with the last row, I would like a similar $array
returned so I can do stuff with it.
/**
* Mage_Core_Model_Resource_Iterator
*/
public function getAllDataFromTable()
{
$ordersCollection = Mage::getModel('custom/table')->getCollection();
Mage::getSingleton('core/resource_iterator')->walk($ordersCollection->getSelect(),
array(array($this, 'orderCallback'))
);
}
public function orderCallback($args)
{
return $args['row'];
}