3

I am loading a collection:

$this->_taxAddress = Mage::getModel('vb_tax/calculation_rate')->getCollection();

And add a filter to this collection:

$this->_taxAddress->addFieldToFilter(
            'region', array(
                'eq' => $address['regionCode']
            )
        );

How can I remove this (or all) filter from the collection again? I don't want to load a new collection every time the filter changes.

I tried

$this->_taxAddress->clear();
$this->_taxAddress->resetData();

But that doesn't seem to have any effects on the filter.

1 Answer 1

8

This is actually a great deal of trouble unless you are willing to parse and reimplement raw SQL. Using the Zend_Db_Select class, you can call:

$where = $collection->getSelect()->getPart(Zend_Db_Select::WHERE);

This gives an order based array of each where part in raw SQL format. Unfortunately there is no counter function of setPart(). So even if you do manage to find the SQL, you are unable to update change the WHERE data... without using the Reflection class.

The closest you can get to this is using the following:

$collection->getSelect()->reset(Zend_Db_Select::WHERE);

Which will only reset the WHERE part of the statement... requiring you to manually add all of the addFieldToFilter() methods again.

On the other hand... if you control the Collection class, you could cache each where condition by the field name as its added by extending the addFieldToFilter() function, and then add your own removeFieldToFilter() function that rebuilds the WHERE without the field. Because you would be unable to edit the default Varien_Data_Collection_Db, it would require you to make this modification to every Collection class that you want this ability.

public function addFieldToFilter($field, $condition=null)
{
    $this->_filterCache[$field] = $condition;
    return parent::addFieldToFilter($field, $condition);
}
2
  • Forget my previous answer, was a long day, I totally overlooked your $collection->getSelect()->reset(Zend_Db_Select::WHERE); part. Yes, that's way easier.
    – empiric
    Commented Nov 9, 2016 at 21:07
  • Addition: you can use getPart(Zend_Db_Select::WHERE), it results an array where you can find the filter you want to remove by key, then use setPart(Zend_Db_Select::WHERE) to replace the conditions with a modied array Commented Nov 16, 2016 at 9:48

Your Answer

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

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