Mage_Catalog_Model_Resource_Eav_Mysql4_Abstract Class Reference

Inheritance diagram for Mage_Catalog_Model_Resource_Eav_Mysql4_Abstract:

Mage_Eav_Model_Entity_Abstract Mage_Core_Model_Resource_Abstract Mage_Eav_Model_Entity_Interface Mage_Catalog_Model_Resource_Eav_Mysql4_Category Mage_Catalog_Model_Resource_Eav_Mysql4_Product

List of all members.

Public Member Functions

 getDefaultStoreId ()

Protected Member Functions

 _getDefaultAttributeModel ()
 _isApplicableAttribute ($object, $attribute)
 _getLoadAttributesSelect ($object, $table)
 _setAttribteValue ($object, $valueRow)
 _insertAttribute ($object, $attribute, $value)
 _updateAttribute ($object, $attribute, $valueId, $value)
 _updateAttributeForStore ($object, $attribute, $value, $storeId)
 _deleteAttributes ($object, $table, $info)
 _getOrigObject ($object)
 _collectOrigData ($object)


Detailed Description

Definition at line 34 of file Abstract.php.


Member Function Documentation

_collectOrigData ( object  )  [protected]

Definition at line 391 of file Abstract.php.

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 //echo "<pre>".print_r($allStores ,1)."</pre>"; exit;
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     }

_deleteAttributes ( object,
table,
info 
) [protected]

Delete entity attribute values

Parameters:
Varien_Object $object
string $table
array $info
Returns:
Varien_Object

Separate attributes by scope

Delete global scope attributes

Delete website scope attributes

Delete store scope attributes

Reimplemented from Mage_Eav_Model_Entity_Abstract.

Definition at line 284 of file Abstract.php.

00285     {
00286         $entityIdField      = $this->getEntityIdField();
00287         $globalValues       = array();
00288         $websiteAttributes  = array();
00289         $storeAttributes    = array();
00290 
00291         /**
00292          * Separate attributes by scope
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          * Delete global scope attributes
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          * Delete website scope attributes
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          * Delete store scope attributes
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     }

_getDefaultAttributeModel (  )  [protected]

Redeclare attribute model

Returns:
string

Reimplemented from Mage_Eav_Model_Entity_Abstract.

Definition at line 41 of file Abstract.php.

00042     {
00043         return 'catalog/resource_eav_attribute';
00044     }

_getLoadAttributesSelect ( object,
table 
) [protected]

Retrieve select object for loading entity attributes values

Join attribute store value

Parameters:
Varien_Object $object
mixed $rowId
Returns:
Zend_Db_Select

This condition is applicable for all cases when we was work in not single store mode, customize some value per specific store view and than back to single store mode. We should load correct values

Reimplemented from Mage_Eav_Model_Entity_Abstract.

Definition at line 73 of file Abstract.php.

00074     {
00075         /**
00076          * This condition is applicable for all cases when we was work in not single
00077          * store mode, customize some value per specific store view and than back
00078          * to single store mode. We should load correct values
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     }

_getOrigObject ( object  )  [protected]

Reimplemented from Mage_Eav_Model_Entity_Abstract.

Definition at line 342 of file Abstract.php.

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     }

_insertAttribute ( object,
attribute,
value 
) [protected]

Insert entity attribute value

Insert attribute value we do only for default store

Parameters:
Varien_Object $object
Mage_Eav_Model_Entity_Attribute_Abstract $attribute
mixed $value
Returns:
Mage_Eav_Model_Entity_Abstract

Reimplemented from Mage_Eav_Model_Entity_Abstract.

Definition at line 145 of file Abstract.php.

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     }

_isApplicableAttribute ( object,
attribute 
) [protected]

Check whether the attribute is Applicable to the object

Parameters:
Varien_Object $object
Mage_Catalog_Model_Resource_Eav_Attribute $attribute
Returns:
boolean

Reimplemented from Mage_Eav_Model_Entity_Abstract.

Definition at line 58 of file Abstract.php.

00059     {
00060         $applyTo = $attribute->getApplyTo();
00061         return count($applyTo) == 0 || in_array($object->getTypeId(), $applyTo);
00062     }

_setAttribteValue ( object,
valueRow 
) [protected]

Initialize attribute value for object

Parameters:
Varien_Object $object
array $valueRow
Returns:
Mage_Eav_Model_Entity_Abstract

Reimplemented from Mage_Eav_Model_Entity_Abstract.

Definition at line 121 of file Abstract.php.

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     }

_updateAttribute ( object,
attribute,
valueId,
value 
) [protected]

Update entity attribute value

Parameters:
Varien_Object $object
Mage_Eav_Model_Entity_Attribute_Abstract $attribute
mixed $valueId
mixed $value
Returns:
Mage_Eav_Model_Entity_Abstract

If we work in single store mode all values should be saved just for default store id In this case we clear all not default values

Update attribute value for store

Update attribute value for website

Reimplemented from Mage_Eav_Model_Entity_Abstract.

Definition at line 190 of file Abstract.php.

00191     {
00192         /**
00193          * If we work in single store mode all values should be saved just
00194          * for default store id
00195          * In this case we clear all not default values
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          * Update attribute value for store
00208          */
00209         if ($attribute->isScopeStore()) {
00210             $this->_updateAttributeForStore($object, $attribute, $value, $object->getStoreId());
00211         }
00212 
00213         /**
00214          * Update attribute value for website
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     }

_updateAttributeForStore ( object,
attribute,
value,
storeId 
) [protected]

Update attribute value for specific store

Parameters:
Mage_Catalog_Model_Abstract $object
object $attribute
mixed $value
int $storeId
Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Abstract

When value for store exist

Definition at line 245 of file Abstract.php.

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          * When value for store exist
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     }

getDefaultStoreId (  ) 

Definition at line 46 of file Abstract.php.

00047     {
00048         return Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID;
00049     }


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:23:42 2009 for Magento by  doxygen 1.5.8