user439441
user439441

Reputation:

Remove where filter from Zend DB Select

How do I remove a where condition on a Zend_DB_Select? I've a Magento collection which I added a where condition to using the addAttributeToFilter method from Varien_Data_Collection_Db. Now I need to remove that where condition. Is this possible?

MORE INFO

I've a Magento widget that loads a collection based on an attribute. The widget does this:

$collection->addAttributeToFilter($attributeCode, array('notnull'->true));

The widget also adds the following filter, to limit results to products that have a name starting with a letter:

collection->addAttributeToFilter('name', array('like'-> $letter.'%'));

However, the client's custom toolbar needs an index of ALL the products by first letter, like [A][B][C][D]...[Z], so when you click on the link, the product collection will be filtered with products starting with that first letter. The problem is that the catalog/product_list_toolbar toolbar block uses the same product collection as the parent catalog/product_list block. So the toolbar's product collection is also filtered by letter.

WORKAROUND

Here's my hacky solution. I added the following code to the widget using PHP's reflection:

$letterCollection = clone $this->_productCollection;
$letterSelect = clone $letterCollection->getSelect();
$reflectionClass = new ReflectionClass($letterCollection);
$reflectionProperty = $reflectionClass->getProperty('_select');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($letterCollection, $letterSelect);
$letterCollection->addAttributeToSelect('name');
$toolbar = $this->getChild('toolbar');
$toolbar->setLetterCollection($letterCollection);

$this->_productCollection->addFieldToFilter('name', array('like' => "$letter%"));

In the extended toolbar (Namespace_Module_Block_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar), I just get the unfiltered collection:

$collection = $this->getLetterCollection();// $this->getCollection() would be the filtered collection

There should really be a way to remove filter from a Magento collection.

Upvotes: 1

Views: 2892

Answers (1)

venca
venca

Reputation: 1222

Just call reset

$select->reset(Zend_Db_Select::WHERE);

Upvotes: 2

Related Questions