addAttributeToFilter on magento return incorrect -
i got problem when overriden mage_catalog_block_product_list class in magento. goal add filter @ overriden class. code
public function getloadedproductcollection() { $collection = parent::_getproductcollection(); $collection->addattributetofilter('model', array('eq' => 'coucu')); var_dump($collection->count()); // return 1 (incorrect) var_dump($collection->getdata()); // return empty (correct) return $collection; }
i need after filter don't have product!
most of time due collection being loaded. have know magento collection load once (to save db access) because there flag _setisloaded
on underlaying varien_data_collection
stop reload it.
what can try
public function getloadedproductcollection() { $collection = parent::_getproductcollection(); var_dump($collection->isloaded()); return $collection; }
if displays true
means collection loaded , addattributetofilter
won't ever executed. , going case because, function not called getloadedproductcollection
no reason.
there 2 ways resolve this.
best : override setcollection
instead
public function setcollection($collection) { $this->_productcollection = $collection; $this->_productcollection->addattributetofilter('model', array('eq' => 'coucu')); return $this; }
the not because collection loaded twice , surcharge db :
public function getloadedproductcollection() { $collection = parent::_getproductcollection(); $collection->clear(); // resets _isloaded flag /** @see varien_data_collection::clear() */ $collection->addattributetofilter('model', array('eq' => 'coucu')); return $collection; }
Comments
Post a Comment