1

I've created a custom module that allows you to add custom labels on product page. So I have a collection of custom objects and I want to filter it but I want to keep the original collection too.

This is my code to understand better:

The model:

class Company_ModuleName_Model_Label extends Mage_Core_Model_Abstract
{
    private $_collection;

    public function __construct()
    {  
        $this->_collection = $this->getCollection();
    }

    public function filterByNew()
    {
         $collection = $this->_collection;
         $collection->addFieldToFilter('type', 'new')->addFieldToFilter('status', 1);

         return $collection;
    }

    public function filterByOld()
    {
         $collection = $this->_collection;
         $collection->addFieldToFilter('type', 'old')->addFieldToFilter('status', 1);

         return $collection;
    }
}

And the controller:

class Company_ModuleName_Adminhtml_LabelController extends Mage_Adminhtml_Controller_Action
{
     public function dosomethingAction()
     {
         $model = Mage::getModel('module/model'); //the collection is defined in __construct

         $new = $model->filterByNew();

         //do something with $new

         $old = $model->filterByOld();

         //do something with $old
     }
}

The problem is that after filtering by new, the original collection is not kept. $this->_collection is now filtered by new, even if I put this line $collection = $this->_collection;. So now if I want to filter by old, it returns nothing because it's already filtered by new.

Is there a way to filter the original collection by both types, new and old, but not in the same filtration, without loading the collection again?

Thanks a lot!

0

Your Answer

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

Browse other questions tagged or ask your own question.