I have a custom module that creates a sql table/collection with data added either by admin or users.
On certain pages this one collection is called a few times with specific filters to output specific data. See example below how i fetch/display the data.
$my_collection_set_A = Mage::getModel('collection/collection')->getCollection();
$my_collection_set_A->addFieldToFilter('created_time', array('gt' => date("Y-m-d H:i:s", strtotime('-1 day'))));
foreach($my_collection_set_A as $set_A_item){
echo $set_A_item->getId();
}
$my_collection_set_B = Mage::getModel('collection/collection')->getCollection();
$my_collection_set_B->addFieldToFilter('created_time', array('gt' => date("Y-m-d H:i:s", strtotime('-1 week'))));
foreach($my_collection_set_B as $set_B_item){
echo $set_B_item->getId();
}
$my_collection_set_C = Mage::getModel('collection/collection')->getCollection();
$my_collection_set_C->addFieldToFilter('created_time', array('gt' => date("Y-m-d H:i:s", strtotime('-1 month'))));
foreach($my_collection_set_C as $set_C_item){
echo $set_C_item->getId();
}
As you can see this is just simple collection filtering but is there a way just to call the collection once and filter later down the track (multiple times, different filters);
//Top of Page code (only call once)
$collection = Mage::getModel('collection/collection')->getCollection();
...
...
...
...
//called later down
$collection->addFieldToFilter('created_time', array('gt' => date("Y-m-d H:i:s", strtotime('-1 month'))));
foreach($collection as $item){
echo $item->getId();
}
...
...
...
//called again later down
$collection->addFieldToFilter('created_time', array('gt' => date("Y-m-d H:i:s", strtotime('-1 day'))));
foreach($collection as $item){
echo $item->getId();
}
...
...
etc
Surely this is possible as this would save alot of overheads (not calling the same collection multiple times, only once) especially as Ive got a lot of data to filter/output from the same collection on one page.