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 class Mage_CatalogIndex_Model_Mysql4_Aggregation extends Mage_Core_Model_Mysql4_Abstract
00028 {
00029 protected $_tagTable;
00030 protected $_toTagTable;
00031
00032
00033
00034
00035 protected function _construct()
00036 {
00037 $this->_init('catalogindex/aggregation', 'aggregation_id');
00038 $this->_tagTable = $this->getTable('catalogindex/aggregation_tag');
00039 $this->_toTagTable = $this->getTable('catalogindex/aggregation_to_tag');
00040 }
00041
00042
00043
00044
00045
00046
00047
00048
00049 public function getCacheData($key, $storeId)
00050 {
00051 $select = $this->_getReadAdapter()->select()
00052 ->from(array('a'=>$this->getMainTable()), 'data')
00053 ->where('a.store_id=?', $storeId)
00054 ->where('a.key=?', $key);
00055 $data = $this->_getReadAdapter()->fetchOne($select);
00056 if ($data) {
00057 $data = unserialize($data);
00058 } else {
00059 $data = array();
00060 }
00061 return $data;
00062 }
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 public function saveCacheData($data, $key, $tags, $storeId)
00074 {
00075 $data = serialize($data);
00076 $tags = $this->_getTagIds($tags);
00077 $select = $this->_getWriteAdapter()->select()
00078 ->from(array('a'=>$this->getMainTable()), $this->getIdFieldName())
00079 ->where('a.store_id=?', $storeId)
00080 ->where('a.key=?', $key);
00081
00082 $id = $this->_getWriteAdapter()->fetchOne($select);
00083 if ($id) {
00084 $this->_getWriteAdapter()->update(
00085 $this->getMainTable(),
00086 array('data'=>$data),
00087 $this->_getWriteAdapter()->quoteInto($this->getIdFieldName().'=?', $id)
00088 );
00089 } else {
00090 $this->_getWriteAdapter()->insert($this->getMainTable(), array(
00091 'store_id' => $storeId,
00092 'created_at'=> $this->formatDate(time()),
00093 'key' => $key,
00094 'data' => $data
00095 ));
00096 $id = $this->_getWriteAdapter()->lastInsertId();
00097 }
00098 $this->_saveTagRelations($id, $tags);
00099 return $this;
00100 }
00101
00102 public function clearCacheData($tags, $storeId)
00103 {
00104 $conditions = array();
00105 if (!$write = $this->_getWriteAdapter()) {
00106 return $this;
00107 }
00108 if (!empty($tags)) {
00109 $tagIds = $this->_getTagIds($tags);
00110 $select = $write->select()
00111 ->from($this->_toTagTable, 'aggregation_id')
00112 ->where('tag_id IN (?)', $tagIds);
00113 $conditions[] = $write->quoteInto('aggregation_id IN ?', $select);
00114 }
00115
00116 if ($storeId !== null) {
00117 $conditions[] = $write->quoteInto('store_id=?', $storeId);
00118 }
00119
00120 $write->delete($this->getMainTable(), implode(' AND ', $conditions));
00121 return $this;
00122 }
00123
00124
00125
00126
00127
00128
00129
00130
00131 protected function _saveTagRelations($aggregationId, $tags)
00132 {
00133 $query = "REPLACE INTO `{$this->_toTagTable}` (aggregation_id, tag_id) VALUES ";
00134 $data = array();
00135 foreach ($tags as $tagId) {
00136 $data[] = $aggregationId.','.$tagId;
00137 }
00138 $query.= '(' . implode('),(', $data) . ')';
00139 $this->_getWriteAdapter()->query($query);
00140 return $this;
00141 }
00142
00143
00144
00145
00146
00147
00148
00149
00150 protected function _getTagIds($tags)
00151 {
00152 if (!is_array($tags)) {
00153 $tags = array($tags);
00154 }
00155
00156 $select = $this->_getReadAdapter()->select()
00157 ->from(array('tags'=>$this->_tagTable), array('tag_code', 'tag_id'))
00158 ->where('tags.tag_code IN (?)', $tags);
00159
00160 $tagIds = $this->_getReadAdapter()->fetchPairs($select);
00161
00162
00163
00164
00165 $newTags = array_diff($tags, array_keys($tagIds));
00166 if (!empty($newTags)) {
00167 $this->_addTags($newTags);
00168 $select->reset(Zend_Db_Select::WHERE)
00169 ->where('tags.tag_code IN (?)', $newTags);
00170 $newTags = $this->_getReadAdapter()->fetchPairs($select);
00171 $tagIds = array_merge($tagIds, $newTags);
00172 }
00173 return $tagIds;
00174 }
00175
00176
00177
00178
00179
00180
00181
00182 protected function _addTags($tags)
00183 {
00184 if (is_array($tags)) {
00185 $tags = array_unique($tags);
00186 foreach ($tags as $index => $tag) {
00187 $tags[$index] = $this->_getWriteAdapter()->quote($tag);
00188 }
00189 $query = "INSERT INTO `{$this->_tagTable}` (tag_code) VALUES (".implode('),(', $tags).")";
00190 $this->_getWriteAdapter()->query($query);
00191 }
00192 else {
00193 $this->_getWriteAdapter()->insert($this->_tagTable, array(
00194 'tag_code' => $tags
00195 ));
00196 }
00197 return $this;
00198 }
00199
00200 public function getProductCategoryPaths($productIds)
00201 {
00202 $select = $this->_getReadAdapter()->select()
00203 ->from(array('cat'=>$this->getTable('catalog/category')), 'path')
00204 ->joinInner(
00205 array('cat_prod'=>$this->getTable('catalog/category_product')),
00206 $this->_getReadAdapter()->quoteInto(
00207 'cat.entity_id=cat_prod.category_id AND cat_prod.product_id IN (?)',
00208 $productIds
00209 ),
00210 array()
00211 );
00212 return $this->_getReadAdapter()->fetchCol($select);
00213 }
00214 }