0

Can we use a fetched Magento collection more than once?

If Yes, then how is it implemented?

For Example:

I have an action in a controller in which I fetched all the products with its required attribute. Now I have to perform filters and sorting to that collection via AJAX.

 public function getProductCollection(){

 $collection = Mage::getModel('catalog/product')
        ->getCollection()
        ->addAttributeToSelect('a1')
        ->addAttributeToSelect('sku')
        ->addAttributeToSelect('a3')
        ->addAttributeToSelect('a4')
        ->addAttributeToSelect('...');

        return $collection;

}

Now I have another action

public function sortcollection(){
/*
    Here I want to apply filter and sorting to the collection fetched in
    above action. It this is not possible than I am open to apply filters
    and sorting in previous action as well but without fetching collection
    again. 
*/
}
2

Yes, it is entirely possible. If collection is not large enough to cause memory issues, you can put it in Mage::registry(), it will be accessible throughout the life-cycle of that request afterwards.

Also, if both functions are in same class, you can put that collection in a class property, this way, your collection will be available only in that class but to all the methods.

In case, you are trying to retain data between multiple requests, I'd suggest using sessions. As mentioned in the comments, sessions are not an option here. So, you can try that with the frontend only. Collection will not be re-used, but I don't think that is going to be any issue anymore, as collection will not be loaded twice in one request cycle.

To achieve that, you can try mimicking what Magento does with category pages for pagination and sorting. Everytime user selects any option in frontend, you will need to retain that data itself in template, either in JS objects or as properties of some special HTML elements.

Providing a code sample would be tough unless you show what methods you are using in frontend and how they are being handled in backend, as there can be 100 possible ways to get this and my sample could be completely different than what you have now. Some changes in your existing code will be little headache than having a total new approach for some minor issue.

Also, looking at the code you supplied, if it's just about working with the multiple actions in same controller with pre-defined parameters(I highly doubt that's the case though!), wrap your logic inside a method that is not an action, and call that method in both of your actions. You'll get identical collection in both cases.

  • Thanks for quick response.. But I have tried it with registry. In First action after fetching collection data I used that registry to display all the results via template. Now from template I am sending an AJAX request to second action and I am unable to use that registry in my second action. – Chetan Oct 8 '15 at 10:12
  • Registry is maintained only for a single request cycle, if you want to retain the data for your second request, I'd suggest using sessions instead. Only session data can be maintained for multiple requests. – Prateek Oct 8 '15 at 10:18
  • My collection consists of 2000 products and it is not a best way to use session as it will use large amount of memory. – Chetan Oct 8 '15 at 10:20
  • In that case, you'll need to retain the data in your template files, based upon which you fired first request, and send them again in second request so that same filters will be applied second time. Only this way, you can achieve what you desire. – Prateek Oct 8 '15 at 10:23
  • Could you please send me any example snippet for that that would be more helpful as I am new to magento ? – Chetan Oct 8 '15 at 10:25
0

you can magento session for that

public function getProductCollection(){

 $collection = Mage::getModel('catalog/product')
        ->getCollection()
        ->addAttributeToSelect('a1')
        ->addAttributeToSelect('sku')
        ->addAttributeToSelect('a3')
        ->addAttributeToSelect('a4')
        ->addAttributeToSelect('...');
Mage::getSingleton('core/session')->setCustomCollection($collection);
        return $collection;

}

you retrieve in function

public function sortcollection(){
$collection=Mage::getSingleton('core/session')->getsCustomCollection();
/*
    Here I want to apply filter and sorting to the collection fetched in
    above action. It this is not possible than I am open to apply filters
    and sorting in previous action as well but without fetching collection
    again. 
*/
}
  • My collection consists of 2000 products and it is not a best way to use session as it will use large amount of memory. – Chetan Oct 15 '15 at 7:12
  • if you don't want to load all the collection then simple store there ids in session $collection->getAllIds(); – Qaisar Satti Oct 15 '15 at 7:15
  • Mage::getSingleton('core/session')->setCustomCollection( $collection->getAllIds()); this will save only the product ids – Qaisar Satti Oct 15 '15 at 7:19
  • Thanks for quick response. Could you please clarify that the above functionality can help in perform the following operations: 1. Sorting of products on the basis of attributes. 2. Filter on collection on attribute basis. 3. Collection related queries. – Chetan Oct 15 '15 at 10:00
  • can you explain what you want in detail? – Qaisar Satti Oct 15 '15 at 10:09

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.