00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 class Mage_CatalogIndex_Model_Mysql4_Attribute extends Mage_CatalogIndex_Model_Mysql4_Abstract
00034 {
00035 protected function _construct()
00036 {
00037 $this->_init('catalogindex/eav', 'index_id');
00038 }
00039
00040 public function getFilteredEntities($attribute, $filter, $entityFilter)
00041 {
00042 $select = $this->_getReadAdapter()->select();
00043
00044 $select
00045 ->from($this->getMainTable(), 'entity_id')
00046 ->distinct(true)
00047 ->where('entity_id in (?)', $entityFilter)
00048 ->where('store_id = ?', $this->getStoreId())
00049 ->where('attribute_id = ?', $attribute->getId())
00050 ->where('value = ?', $filter);
00051
00052 return $this->_getReadAdapter()->fetchCol($select);
00053 }
00054
00055 public function getCount($attribute, $entitySelect)
00056 {
00057 $select = clone $entitySelect;
00058 $select->reset(Zend_Db_Select::COLUMNS);
00059 $select->reset(Zend_Db_Select::ORDER);
00060 $select->reset(Zend_Db_Select::LIMIT_COUNT);
00061 $select->reset(Zend_Db_Select::LIMIT_OFFSET);
00062
00063 $fields = array('count'=>'COUNT(index.entity_id)', 'index.value');
00064
00065 $select->from('', $fields)
00066 ->join(array('index'=>$this->getMainTable()), 'index.entity_id=e.entity_id', array())
00067 ->where('index.store_id = ?', $this->getStoreId())
00068 ->where('index.attribute_id = ?', $attribute->getId())
00069 ->group('index.value');
00070
00071 $select = $select->__toString();
00072
00073 $result = $this->_getReadAdapter()->fetchAll($select);
00074
00075 $counts = array();
00076 foreach ($result as $row) {
00077 $counts[$row['value']] = $row['count'];
00078 }
00079 return $counts;
00080 }
00081
00082 public function applyFilterToCollection($collection, $attribute, $value)
00083 {
00084
00085
00086
00087
00088
00089
00090
00091
00092 $alias = 'attr_index_'.$attribute->getId();
00093 $collection->getSelect()->join(
00094 array($alias => $this->getMainTable()),
00095 $alias.'.entity_id=e.entity_id',
00096 array()
00097 )
00098 ->where($alias.'.store_id = ?', $this->getStoreId())
00099 ->where($alias.'.attribute_id = ?', $attribute->getId())
00100 ->where($alias.'.value = ?', $value);
00101 return $this;
00102 }
00103 }