/** * @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection * @param Morphes_Filters_Model_Filter_Attribute $model * @return mixed */ public function countOnCollection($collection, $model) { $select = $collection->getSelect(); // reset columns, order and limitation conditions $select->reset(Zend_Db_Select::COLUMNS); $select->reset(Zend_Db_Select::ORDER); $select->reset(Zend_Db_Select::GROUP); $select->reset(Zend_Db_Select::LIMIT_COUNT); $select->reset(Zend_Db_Select::LIMIT_OFFSET); $connection = $this->_getReadAdapter(); $attribute = $model->getAttributeModel(); $tableAlias = $attribute->getAttributeCode() . '_idx'; $conditions = array("{$tableAlias}.entity_id = e.entity_id", $connection->quoteInto("{$tableAlias}.attribute_id = ?", $attribute->getAttributeId()), $connection->quoteInto("{$tableAlias}.store_id = ?", $model->getStoreId())); $select->join(array($tableAlias => $this->getMainTable()), join(' AND ', $conditions), array('value', 'count' => "COUNT(DISTINCT {$tableAlias}.entity_id)"))->group("{$tableAlias}.value"); return $connection->fetchPairs($select); }