2

Consider the below code:

$productCollection123 = Mage::getModel('catalog/product')->getCollection() ->addStoreFilter(Mage::app()->getStore()) ->addAttributeToFilter('type_id', array('eq' => 'simple')) ->addAttributeToFilter('color',5); 

so the resultant count is count($productCollection123) = 57, now is it possible to do a removeAttributeFromFilter so that it removes the condition 'color'=5 from the current collection result?

Note: it should not remove condition 'type_id'='simple'

2 Answers 2

3

No, you have to construct a new query. Once you hit the DB with a query with conditions, you can't just remove a condition and have new data, you have to construct a new query.

Sign up to request clarification or add additional context in comments.

Comments

1

Afaik standard Magento does not have any methods to explicitely remove filters (besides clear(), which resets all filters and unloads the collection).

And to be honest, it does not make much sense to remove filters after a collection already has been loaded. At least to me.

The only thing coming to my mind would be to build a collection having the minimum filters (your 2nd one), then clone it and extend the clone with the maximum filters (your 1st one).

This would save you some code lines, but lacks readability/maintainability.

5 Comments

if i clone a collection, will the system do another database call to fill the collection or only keep the data from collection from which its cloned?
Afaik product collections in Magento do not define any magic __clone() method, so usage of the PHP clone` keyword will just create a 1:1 copy of the cloned product. So cloning itself will not produce another query.
In my case, i have created a clone and then adding fields to filter like below $productCollection123 = Mage::getModel('catalog/product')->getCollection() ->addStoreFilter(Mage::app()->getStore()) ->addAttributeToFilter('type_id', array('eq' => 'simple'))->addAttributeToFilter('color',5); and cloning it $productCollection123Clone = clone $productCollection123; and adding filter $productCollection123Clone->clear()->addAttributeToFilter('pricee',30)->load(); will this reduce database calls made as compared to intialize a collection and adding attributes to filter each time?
No, it wouldn't - but that's comparing apples with oranges anyway. The code within your last comment will produce 1 query and load 1 collection. Initializing two collections and adding filters to them will produce 0 queries and load 0 collections, due to Magento's lazy loading. The first real access to the collection's data (using load(), foreach(), count(), etc.) will trigger lazy loading and produce 1 query per collection. To save queries, you could query only one (the bigger) collection. Then do your further filtering within memory only.
how do i do it? do i have to do like $productCollection123 = Mage::getModel('catalog/product')->getCollection() ->addStoreFilter(Mage::app()->getStore()) ->addAttributeToFilter('type_id', array('eq' => 'simple')) first load this collection and add filters to it?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.