3

Is it possible to check which filters have been applied to a given collection.

For example:

$collection = Mage::getModel('sales/order')->getCollection();
$collection->addFieldToFilter('state', 'processing');
$collection->addFieldToFilter('status', 'processing2');

I want to get an array of all filters applied at this point:

array(
    'state' => 'processing',
    'status' => 'processing2'
)

The closest i can get so far seems to be:

$collection->getSelect()->getPart('where');

But this is not exactly what I am looking for as it does also provide the AND etc.

For example:

array(2) {
    [0] => string(22) "(state = 'processing')"
    [1] => string(28) "AND (status = 'processing2')"
}
5

As you can see in \Varien_Data_Collection_Db::addFieldToFilter there is no way to achieve what you want, out of the box:

public function addFieldToFilter($field, $condition = null)
{
    if (!is_array($field)) {
        $resultCondition = $this->_translateCondition($field, $condition);
    } else {
        $conditions = array();
        foreach ($field as $key => $currField) {
            $conditions[] = $this->_translateCondition(
                $currField,
                isset($condition[$key]) ? $condition[$key] : null
            );
        }

        $resultCondition = '(' . join(') ' . Zend_Db_Select::SQL_OR . ' (', $conditions) . ')';
    }

    $this->_select->where($resultCondition);

    return $this;
}

Because the filter is translated to a where condition and added to the Zend_Db_Select object.

You can change the collection to use the protected $_filters = array(); property instead of applying the filters directly, then you can just get the filter, if you have access to this property.

This works, because \Varien_Data_Collection_Db::load calls \Varien_Data_Collection_Db::_renderFilters.

  • yes,there are no way to achieve it.Thanks for your descrtion – Amit Bera Jun 15 '15 at 11:16

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.