1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: abstract class Mage_Catalog_Model_Resource_Product_Indexer_Eav_Abstract
36: extends Mage_Catalog_Model_Resource_Product_Indexer_Abstract
37: {
38: 39: 40: 41: 42: 43:
44: public function reindexAll()
45: {
46: $this->useIdxTable(true);
47: $this->beginTransaction();
48: try {
49: $this->clearTemporaryIndexTable();
50: $this->_prepareIndex();
51: $this->_prepareRelationIndex();
52: $this->_removeNotVisibleEntityFromIndex();
53:
54: $this->syncData();
55: $this->commit();
56: } catch (Exception $e) {
57: $this->rollBack();
58: throw $e;
59: }
60:
61: return $this;
62: }
63:
64: 65: 66: 67: 68: 69: 70: 71:
72: public function reindexEntities($processIds)
73: {
74: $adapter = $this->_getWriteAdapter();
75:
76: $this->clearTemporaryIndexTable();
77:
78: if (!is_array($processIds)) {
79: $processIds = array($processIds);
80: }
81:
82: $parentIds = $this->getRelationsByChild($processIds);
83: if ($parentIds) {
84: $processIds = array_unique(array_merge($processIds, $parentIds));
85: }
86: $childIds = $this->getRelationsByParent($processIds);
87: if ($childIds) {
88: $processIds = array_unique(array_merge($processIds, $childIds));
89: }
90:
91: $this->_prepareIndex($processIds);
92: $this->_prepareRelationIndex($processIds);
93: $this->_removeNotVisibleEntityFromIndex();
94:
95: $adapter->beginTransaction();
96: try {
97:
98: $where = $adapter->quoteInto('entity_id IN(?)', $processIds);
99: $adapter->delete($this->getMainTable(), $where);
100:
101:
102: $this->useDisableKeys(false);
103: $this->insertFromTable($this->getIdxTable(), $this->getMainTable());
104: $this->useDisableKeys(true);
105:
106: $adapter->commit();
107: } catch (Exception $e) {
108: $adapter->rollBack();
109: throw $e;
110: }
111:
112: return $this;
113: }
114:
115: 116: 117: 118: 119: 120: 121: 122: 123:
124: public function reindexAttribute($attributeId, $isIndexable = true)
125: {
126: if (!$isIndexable) {
127: $this->_removeAttributeIndexData($attributeId);
128: } else {
129: $this->clearTemporaryIndexTable();
130:
131: $this->_prepareIndex(null, $attributeId);
132: $this->_prepareRelationIndex();
133: $this->_removeNotVisibleEntityFromIndex();
134:
135: $this->_synchronizeAttributeIndexData($attributeId);
136: }
137:
138: return $this;
139: }
140:
141: 142: 143: 144: 145: 146:
147: abstract protected function _prepareIndex($entityIds = null, $attributeId = null);
148:
149: 150: 151: 152: 153:
154: protected function _removeNotVisibleEntityFromIndex()
155: {
156: $write = $this->_getWriteAdapter();
157: $idxTable = $this->getIdxTable();
158:
159: $select = $write->select()
160: ->from($idxTable, null);
161:
162: $condition = $write->quoteInto('=?',Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
163: $this->_addAttributeToSelect(
164: $select,
165: 'visibility',
166: $idxTable . '.entity_id',
167: $idxTable . '.store_id',
168: $condition
169: );
170:
171: $query = $select->deleteFromSelect($idxTable);
172: $write->query($query);
173:
174: return $this;
175: }
176:
177: 178: 179: 180: 181: 182:
183: protected function _prepareRelationIndex($parentIds = null)
184: {
185: $write = $this->_getWriteAdapter();
186: $idxTable = $this->getIdxTable();
187:
188: $select = $write->select()
189: ->from(array('l' => $this->getTable('catalog/product_relation')), 'parent_id')
190: ->join(
191: array('cs' => $this->getTable('core/store')),
192: '',
193: array())
194: ->join(
195: array('i' => $idxTable),
196: 'l.child_id = i.entity_id AND cs.store_id = i.store_id',
197: array('attribute_id', 'store_id', 'value'))
198: ->group(array(
199: 'l.parent_id', 'i.attribute_id', 'i.store_id', 'i.value'
200: ));
201: if (!is_null($parentIds)) {
202: $select->where('l.parent_id IN(?)', $parentIds);
203: }
204:
205: 206: 207:
208: Mage::dispatchEvent('prepare_catalog_product_index_select', array(
209: 'select' => $select,
210: 'entity_field' => new Zend_Db_Expr('l.parent_id'),
211: 'website_field' => new Zend_Db_Expr('cs.website_id'),
212: 'store_field' => new Zend_Db_Expr('cs.store_id')
213: ));
214:
215: $query = $write->insertFromSelect($select, $idxTable, array(), Varien_Db_Adapter_Interface::INSERT_IGNORE);
216: $write->query($query);
217:
218: return $this;
219: }
220:
221: 222: 223: 224: 225: 226:
227: protected function _getIndexableAttributesCondition()
228: {
229: $conditions = array(
230: 'ca.is_filterable_in_search > 0',
231: 'ca.is_visible_in_advanced_search > 0',
232: 'ca.is_filterable > 0'
233: );
234:
235: return implode(' OR ', $conditions);
236: }
237:
238: 239: 240: 241: 242: 243:
244: protected function _removeAttributeIndexData($attributeId)
245: {
246: $adapter = $this->_getWriteAdapter();
247: $adapter->beginTransaction();
248: try {
249: $where = $adapter->quoteInto('attribute_id = ?', $attributeId);
250: $adapter->delete($this->getMainTable(), $where);
251: $adapter->commit();
252: } catch (Exception $e) {
253: $adapter->rollback();
254: throw $e;
255: }
256:
257: return $this;
258: }
259:
260: 261: 262: 263: 264: 265: 266:
267: protected function _synchronizeAttributeIndexData($attributeId)
268: {
269: $adapter = $this->_getWriteAdapter();
270: $adapter->beginTransaction();
271: try {
272:
273: $where = $adapter->quoteInto('attribute_id = ?', $attributeId);
274: $adapter->delete($this->getMainTable(), $where);
275:
276:
277: $this->insertFromTable($this->getIdxTable(), $this->getMainTable());
278:
279: $adapter->commit();
280: } catch (Exception $e) {
281: $adapter->rollback();
282: throw $e;
283: }
284:
285: return $this;
286: }
287: }
288: