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
00034 abstract class Mage_Catalog_Model_Resource_Eav_Mysql4_Abstract extends Mage_Eav_Model_Entity_Abstract
00035 {
00036
00037
00038
00039
00040
00041 protected function _getDefaultAttributeModel()
00042 {
00043 return 'catalog/resource_eav_attribute';
00044 }
00045
00046 public function getDefaultStoreId()
00047 {
00048 return Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID;
00049 }
00050
00051
00052
00053
00054
00055
00056
00057
00058 protected function _isApplicableAttribute ($object, $attribute)
00059 {
00060 $applyTo = $attribute->getApplyTo();
00061 return count($applyTo) == 0 || in_array($object->getTypeId(), $applyTo);
00062 }
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 protected function _getLoadAttributesSelect($object, $table)
00074 {
00075
00076
00077
00078
00079
00080 if (Mage::app()->isSingleStoreMode()) {
00081 $storeId = Mage::app()->getStore(true)->getId();
00082 } else {
00083 $storeId = $object->getStoreId();
00084 }
00085
00086 $select = $this->_read->select()
00087 ->from(array('default' => $table));
00088 if ($setId = $object->getAttributeSetId()) {
00089 $select->join(
00090 array('set_table' => $this->getTable('eav/entity_attribute')),
00091 'default.attribute_id=set_table.attribute_id AND '
00092 . 'set_table.attribute_set_id=' . intval($setId),
00093 array()
00094 );
00095 }
00096
00097 $joinCondition = 'main.attribute_id=default.attribute_id AND '
00098 . $this->_read->quoteInto('main.store_id=? AND ', intval($storeId))
00099 . $this->_read->quoteInto('main.'.$this->getEntityIdField() . '=?', $object->getId());
00100
00101 $select->joinLeft(
00102 array('main' => $table),
00103 $joinCondition,
00104 array(
00105 'store_value_id' => 'value_id',
00106 'store_value' => 'value'
00107 ))
00108 ->where('default.'.$this->getEntityIdField() . '=?', $object->getId())
00109 ->where('default.store_id=?', $this->getDefaultStoreId());
00110
00111 return $select;
00112 }
00113
00114
00115
00116
00117
00118
00119
00120
00121 protected function _setAttribteValue($object, $valueRow)
00122 {
00123 parent::_setAttribteValue($object, $valueRow);
00124 if ($attribute = $this->getAttribute($valueRow['attribute_id'])) {
00125 $attributeCode = $attribute->getAttributeCode();
00126 if (isset($valueRow['store_value'])) {
00127 $object->setAttributeDefaultValue($attributeCode, $valueRow['value']);
00128 $object->setData($attributeCode, $valueRow['store_value']);
00129 $attribute->getBackend()->setValueId($valueRow['store_value_id']);
00130 }
00131 }
00132 return $this;
00133 }
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 protected function _insertAttribute($object, $attribute, $value)
00146 {
00147 $entityIdField = $attribute->getBackend()->getEntityIdField();
00148 $row = array(
00149 $entityIdField => $object->getId(),
00150 'entity_type_id'=> $object->getEntityTypeId(),
00151 'attribute_id' => $attribute->getId(),
00152 'value' => $this->_prepareValueForSave($value, $attribute),
00153 'store_id' => $this->getDefaultStoreId()
00154 );
00155 $fields = array();
00156 $values = array();
00157 foreach ($row as $k => $v) {
00158 $fields[] = $this->_getWriteAdapter()->quoteIdentifier('?', $k);
00159 $values[] = $this->_getWriteAdapter()->quoteInto('?', $v);
00160 }
00161 $sql = sprintf('INSERT IGNORE INTO %s (%s) VALUES(%s)',
00162 $this->_getWriteAdapter()->quoteIdentifier($attribute->getBackend()->getTable()),
00163 join(',', array_keys($row)),
00164 join(',', $values));
00165 $this->_getWriteAdapter()->query($sql);
00166 if (!$lastId = $this->_getWriteAdapter()->lastInsertId()) {
00167 $select = $this->_getReadAdapter()->select()
00168 ->from($attribute->getBackend()->getTable(), 'value_id')
00169 ->where($entityIdField . '=?', $row[$entityIdField])
00170 ->where('entity_type_id=?', $row['entity_type_id'])
00171 ->where('attribute_id=?', $row['attribute_id'])
00172 ->where('store_id=?', $row['store_id']);
00173 $lastId = $select->query()->fetchColumn();
00174 }
00175 if ($object->getStoreId() != $this->getDefaultStoreId()) {
00176 $this->_updateAttribute($object, $attribute, $lastId, $value);
00177 }
00178 return $this;
00179 }
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190 protected function _updateAttribute($object, $attribute, $valueId, $value)
00191 {
00192
00193
00194
00195
00196
00197 if (Mage::app()->isSingleStoreMode()) {
00198 $this->_getWriteAdapter()->delete(
00199 $attribute->getBackend()->getTable(),
00200 $this->_getWriteAdapter()->quoteInto('attribute_id=?', $attribute->getId()) .
00201 $this->_getWriteAdapter()->quoteInto(' AND entity_id=?', $object->getId()) .
00202 $this->_getWriteAdapter()->quoteInto(' AND store_id!=?', Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID)
00203 );
00204 }
00205
00206
00207
00208
00209 if ($attribute->isScopeStore()) {
00210 $this->_updateAttributeForStore($object, $attribute, $value, $object->getStoreId());
00211 }
00212
00213
00214
00215
00216 elseif ($attribute->isScopeWebsite()) {
00217 if ($object->getStoreId() == 0) {
00218 $this->_updateAttributeForStore($object, $attribute, $value, $object->getStoreId());
00219 } else {
00220 if (is_array($object->getWebsiteStoreIds())) {
00221 foreach ($object->getWebsiteStoreIds() as $storeId) {
00222 $this->_updateAttributeForStore($object, $attribute, $value, $storeId);
00223 }
00224 }
00225 }
00226 }
00227 else {
00228 $this->_getWriteAdapter()->update($attribute->getBackend()->getTable(),
00229 array('value' => $this->_prepareValueForSave($value, $attribute)),
00230 'value_id='.(int)$valueId
00231 );
00232 }
00233 return $this;
00234 }
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245 protected function _updateAttributeForStore($object, $attribute, $value, $storeId)
00246 {
00247 $entityIdField = $attribute->getBackend()->getEntityIdField();
00248 $select = $this->_getWriteAdapter()->select()
00249 ->from($attribute->getBackend()->getTable(), 'value_id')
00250 ->where('entity_type_id=?', $object->getEntityTypeId())
00251 ->where("$entityIdField=?",$object->getId())
00252 ->where('store_id=?', $storeId)
00253 ->where('attribute_id=?', $attribute->getId());
00254
00255
00256
00257 if ($valueId = $this->_getWriteAdapter()->fetchOne($select)) {
00258 $this->_getWriteAdapter()->update($attribute->getBackend()->getTable(),
00259 array('value' => $this->_prepareValueForSave($value, $attribute)),
00260 'value_id='.$valueId
00261 );
00262 }
00263 else {
00264 $this->_getWriteAdapter()->insert($attribute->getBackend()->getTable(), array(
00265 $entityIdField => $object->getId(),
00266 'entity_type_id'=> $object->getEntityTypeId(),
00267 'attribute_id' => $attribute->getId(),
00268 'value' => $this->_prepareValueForSave($value, $attribute),
00269 'store_id' => $storeId
00270 ));
00271 }
00272
00273 return $this;
00274 }
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284 protected function _deleteAttributes($object, $table, $info)
00285 {
00286 $entityIdField = $this->getEntityIdField();
00287 $globalValues = array();
00288 $websiteAttributes = array();
00289 $storeAttributes = array();
00290
00291
00292
00293
00294 foreach ($info as $itemData) {
00295 $attribute = $this->getAttribute($itemData['attribute_id']);
00296 if ($attribute->isScopeStore()) {
00297 $storeAttributes[] = $itemData['attribute_id'];
00298 }
00299 elseif ($attribute->isScopeWebsite()) {
00300 $websiteAttributes[] = $itemData['attribute_id'];
00301 }
00302 else {
00303 $globalValues[] = $itemData['value_id'];
00304 }
00305 }
00306
00307
00308
00309
00310 if (!empty($globalValues)) {
00311 $condition = $this->_getWriteAdapter()->quoteInto('value_id IN (?)', $globalValues);
00312 $this->_getWriteAdapter()->delete($table, $condition);
00313 }
00314
00315 $condition = $this->_getWriteAdapter()->quoteInto("$entityIdField=?", $object->getId())
00316 . $this->_getWriteAdapter()->quoteInto(' AND entity_type_id=?', $object->getEntityTypeId());
00317
00318
00319
00320 if (!empty($websiteAttributes)) {
00321 $storeIds = $object->getWebsiteStoreIds();
00322 if (!empty($storeIds)) {
00323 $delCondition = $condition
00324 . $this->_getWriteAdapter()->quoteInto(' AND attribute_id IN(?)', $websiteAttributes)
00325 . $this->_getWriteAdapter()->quoteInto(' AND store_id IN(?)', $storeIds);
00326 $this->_getWriteAdapter()->delete($table, $delCondition);
00327 }
00328 }
00329
00330
00331
00332
00333 if (!empty($storeAttributes)) {
00334 $delCondition = $condition
00335 . $this->_getWriteAdapter()->quoteInto(' AND attribute_id IN(?)', $storeAttributes)
00336 . $this->_getWriteAdapter()->quoteInto(' AND store_id =?', $object->getStoreId());
00337 $this->_getWriteAdapter()->delete($table, $delCondition);;
00338 }
00339 return $this;
00340 }
00341
00342 protected function _getOrigObject($object)
00343 {
00344 $className = get_class($object);
00345 $origObject = new $className();
00346 $origObject->setData(array());
00347 $origObject->setStoreId($object->getStoreId());
00348 $this->load($origObject, $object->getData($this->getEntityIdField()));
00349 return $origObject;
00350 }
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391 protected function _collectOrigData($object)
00392 {
00393 $this->loadAllAttributes($object);
00394
00395 if ($this->getUseDataSharing()) {
00396 $storeId = $object->getStoreId();
00397 } else {
00398 $storeId = $this->getStoreId();
00399 }
00400
00401 $allStores = Mage::getConfig()->getStoresConfigByPath('system/store/id', array(), 'code');
00402
00403 $data = array();
00404
00405 foreach ($this->getAttributesByTable() as $table=>$attributes) {
00406 $entityIdField = current($attributes)->getBackend()->getEntityIdField();
00407
00408 $select = $this->_read->select()
00409 ->from($table)
00410 ->where($this->getEntityIdField()."=?", $object->getId());
00411
00412 $where = $this->_read->quoteInto("store_id=?", $storeId);
00413
00414 $globalAttributeIds = array();
00415 foreach ($attributes as $attrCode=>$attr) {
00416 if ($attr->getIsGlobal()) {
00417 $globalAttributeIds[] = $attr->getId();
00418 }
00419 }
00420 if (!empty($globalAttributeIds)) {
00421 $where .= ' or '.$this->_read->quoteInto('attribute_id in (?)', $globalAttributeIds);
00422 }
00423 $select->where($where);
00424
00425 $values = $this->_read->fetchAll($select);
00426
00427 if (empty($values)) {
00428 continue;
00429 }
00430 foreach ($values as $row) {
00431 $data[$this->getAttribute($row['attribute_id'])->getName()][$row['store_id']] = $row;
00432 }
00433 foreach ($attributes as $attrCode=>$attr) {
00434
00435 }
00436 }
00437
00438 return $data;
00439 }
00440
00441 }