2

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.

1 Answer 1

1

Try this

<?php
  //Top of Page code (only call once)
$collection1 = $collection2 = $collection3 = Mage::getModel('collection/collection')->getCollection();


//called later down
$collection1->getSelect()->reset('where');
$collection1->addFieldToFilter('created_time', array('gt' => date("Y-m-d H:i:s", strtotime('-1 month'))));
foreach($collection1 as $item){
echo $item->getId();
}


//called again later down
$collection2->getSelect()->reset('where');
$collection2->addFieldToFilter('created_time', array('gt' => date("Y-m-d H:i:s", strtotime('-1 day'))));
foreach($collection2 as $item){
echo $item->getId();
}

//called again later down
$collection3->getSelect()->reset('where');
$collection3->addFieldToFilter('created_time', array('gt' => date("Y-m-d H:i:s", strtotime('-1 week'))));
foreach($collection3 as $item){
echo $item->getId();
}
7
  • This may work to get results with the said filters, but I'm trying to get the results for each filter separately in their respective foreach sets from one collection call instead of calling the collection multiple times..
    – BENN1TH
    Jan 11, 2016 at 8:37
  • updated ans pls check now... :)
    – Abdul
    Jan 11, 2016 at 9:17
  • Tested the above and sorry doesn't work. Each of the "foreach()" just echos out exact data as each other where as the filter "-1 day" should only output a few IDs..
    – BENN1TH
    Jan 11, 2016 at 10:31
  • updates ans check now
    – Abdul
    Jan 11, 2016 at 10:39
  • Getting "There has been an error processing your request" & under /var/report/ states in the file "Unrecognized method 'resetData()'"
    – BENN1TH
    Jan 11, 2016 at 10:59

Your Answer

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

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