Mage_Eav_Model_Mysql4_Entity_Attribute Class Reference

Inheritance diagram for Mage_Eav_Model_Mysql4_Entity_Attribute:

Mage_Core_Model_Mysql4_Abstract Mage_Core_Model_Resource_Abstract

List of all members.

Public Member Functions

 loadByCode (Mage_Core_Model_Abstract $object, $entityTypeId, $code)
 deleteEntity (Mage_Core_Model_Abstract $object)
 saveInSetIncluding (Mage_Core_Model_Abstract $object)
 isUsedBySuperProducts (Mage_Core_Model_Abstract $object, $attributeSet=null)
 getIdByCode ($entityType, $code)
 getAttributeCodesByFrontendType ($type)
 getFlatUpdateSelect (Mage_Eav_Model_Entity_Attribute_Abstract $attribute, $store)
 describeTable ($table)

Protected Member Functions

 _construct ()
 _initUniqueFields ()
 _loadTypeAttributes ($entityTypeId)
 _beforeSave (Mage_Core_Model_Abstract $object)
 _afterSave (Mage_Core_Model_Abstract $object)
 _saveOption (Mage_Core_Model_Abstract $object)

Static Protected Attributes

static $_entityAttributes = null


Detailed Description

Definition at line 34 of file Attribute.php.


Member Function Documentation

_afterSave ( Mage_Core_Model_Abstract object  )  [protected]

Enter description here...

Parameters:
Mage_Core_Model_Abstract $object
Returns:
Mage_Eav_Model_Mysql4_Entity_Attribute

Reimplemented from Mage_Core_Model_Mysql4_Abstract.

Definition at line 209 of file Attribute.php.

00210     {
00211         $this->saveInSetIncluding($object)
00212             ->_saveOption($object);
00213         return parent::_afterSave($object);
00214     }

_beforeSave ( Mage_Core_Model_Abstract object  )  [protected]

Enter description here...

Parameters:
Mage_Core_Model_Abstract $object
Returns:
Mage_Eav_Model_Mysql4_Entity_Attribute

Todo:
need use default source model of entity type !!!

Reimplemented from Mage_Core_Model_Mysql4_Abstract.

Definition at line 164 of file Attribute.php.

00165     {
00166         $frontendLabel = $object->getFrontendLabel();
00167         if (is_array($frontendLabel)) {
00168             if (!isset($frontendLabel[0]) || is_null($frontendLabel[0]) || $frontendLabel[0]=='') {
00169                 Mage::throwException(Mage::helper('eav')->__('Frontend label is not defined'));
00170             }
00171             $object->setFrontendLabel($frontendLabel[0]);
00172 
00173             if ($object->getData('modulePrefix')) {
00174                 $str = $object->getData('modulePrefix') . Mage_Core_Model_Translate::SCOPE_SEPARATOR . $frontendLabel[0];
00175             }
00176             else {
00177                 $str = $frontendLabel[0];
00178             }
00179             Mage::getModel('core/translate_string')
00180                 ->setString($str)
00181                 ->setTranslate($frontendLabel[0])
00182                 ->setStoreTranslations($frontendLabel)
00183                 ->save();
00184         }
00185         $applyTo = $object->getApplyTo();
00186 
00187         if (is_array($applyTo)) {
00188             $object->setApplyTo(implode(',', $applyTo));
00189         }
00190 
00191         /**
00192          * @todo need use default source model of entity type !!!
00193          */
00194         if (!$object->getId()) {
00195             if ($object->getFrontendInput()=='select') {
00196                 $object->setSourceModel('eav/entity_attribute_source_table');
00197             }
00198         }
00199 
00200         return parent::_beforeSave($object);
00201     }

_construct (  )  [protected]

Resource initialization

Reimplemented from Mage_Core_Model_Resource_Abstract.

Definition at line 38 of file Attribute.php.

00039     {
00040         $this->_init('eav/attribute', 'attribute_id');
00041     }

_initUniqueFields (  )  [protected]

Initialize unique fields

Returns:
Mage_Core_Model_Mysql4_Abstract

Reimplemented from Mage_Core_Model_Mysql4_Abstract.

Definition at line 48 of file Attribute.php.

00049     {
00050         $this->_uniqueFields = array(array(
00051             'field' => array('attribute_code','entity_type_id'),
00052             'title' => Mage::helper('eav')->__('Attribute with the same code')
00053         ));
00054         return $this;
00055     }

_loadTypeAttributes ( entityTypeId  )  [protected]

Definition at line 57 of file Attribute.php.

00058     {
00059         if (!isset(self::$_entityAttributes[$entityTypeId])) {
00060             $select = $this->_getReadAdapter()->select()->from($this->getMainTable())
00061                 ->where('entity_type_id=?', $entityTypeId);
00062             $data = $this->_getReadAdapter()->fetchAll($select);
00063             foreach ($data as $row) {
00064                 self::$_entityAttributes[$entityTypeId][$row['attribute_code']] = $row;
00065             }
00066         }
00067         return $this;
00068     }

_saveOption ( Mage_Core_Model_Abstract object  )  [protected]

Enter description here...

Parameters:
Mage_Core_Model_Abstract $object
Returns:
Mage_Eav_Model_Mysql4_Entity_Attribute

Definition at line 256 of file Attribute.php.

00257     {
00258         $option = $object->getOption();
00259         if (is_array($option)) {
00260             $write = $this->_getWriteAdapter();
00261             $optionTable        = $this->getTable('attribute_option');
00262             $optionValueTable   = $this->getTable('attribute_option_value');
00263             $stores = Mage::getModel('core/store')
00264                 ->getResourceCollection()
00265                 ->setLoadDefault(true)
00266                 ->load();
00267 
00268             if (isset($option['value'])) {
00269                 $attributeDefaultValue = array();
00270                 if (!is_array($object->getDefault())) {
00271                     $object->setDefault(array());
00272                 }
00273 
00274                 foreach ($option['value'] as $optionId => $values) {
00275                     $intOptionId = (int) $optionId;
00276                     if (!empty($option['delete'][$optionId])) {
00277                         if ($intOptionId) {
00278                             $condition = $write->quoteInto('option_id=?', $intOptionId);
00279                             $write->delete($optionTable, $condition);
00280                         }
00281 
00282                         continue;
00283                     }
00284 
00285                     if (!$intOptionId) {
00286                         $data = array(
00287                            'attribute_id'  => $object->getId(),
00288                            'sort_order'    => isset($option['order'][$optionId]) ? $option['order'][$optionId] : 0,
00289                         );
00290                         $write->insert($optionTable, $data);
00291                         $intOptionId = $write->lastInsertId();
00292                     }
00293                     else {
00294                         $data = array(
00295                            'sort_order'    => isset($option['order'][$optionId]) ? $option['order'][$optionId] : 0,
00296                         );
00297                         $write->update($optionTable, $data, $write->quoteInto('option_id=?', $intOptionId));
00298                     }
00299 
00300                     if (in_array($optionId, $object->getDefault())) {
00301                         if ($object->getFrontendInput() == 'multiselect') {
00302                             $attributeDefaultValue[] = $intOptionId;
00303                         } else if ($object->getFrontendInput() == 'select') {
00304                             $attributeDefaultValue = array($intOptionId);
00305                         }
00306                     }
00307 
00308 
00309                     // Default value
00310                     if (!isset($values[0])) {
00311                         Mage::throwException(Mage::helper('eav')->__('Default option value is not defined'));
00312                     }
00313 
00314                     $write->delete($optionValueTable, $write->quoteInto('option_id=?', $intOptionId));
00315                     foreach ($stores as $store) {
00316                         if (isset($values[$store->getId()]) && (!empty($values[$store->getId()]) || $values[$store->getId()] == "0")) {
00317                             $data = array(
00318                                 'option_id' => $intOptionId,
00319                                 'store_id'  => $store->getId(),
00320                                 'value'     => $values[$store->getId()],
00321                             );
00322                             $write->insert($optionValueTable, $data);
00323                         }
00324                     }
00325                 }
00326 
00327                 $write->update($this->getMainTable(), array(
00328                     'default_value' => implode(',', $attributeDefaultValue)
00329                 ), $write->quoteInto($this->getIdFieldName() . '=?', $object->getId()));
00330             }
00331         }
00332         return $this;
00333     }

deleteEntity ( Mage_Core_Model_Abstract object  ) 

Enter description here...

Parameters:
Mage_Core_Model_Abstract $object
Returns:
Mage_Eav_Model_Mysql4_Entity_Attribute

Delete attribute values

Todo:
!!!! need fix retrieving attribute entity, this realization is temprary

Definition at line 119 of file Attribute.php.

00120     {
00121         $write = $this->_getWriteAdapter();
00122         $condition = $write->quoteInto($this->getTable('entity_attribute').'.entity_attribute_id = ?', $object->getEntityAttributeId());
00123         /**
00124          * Delete attribute values
00125          */
00126         $select = $write->select()
00127             ->from($this->getTable('entity_attribute'))
00128             ->where($condition);
00129         $data = $write->fetchRow($select);
00130         if (!empty($data)) {
00131             /**
00132              * @todo !!!! need fix retrieving attribute entity, this realization is temprary
00133              */
00134             $attribute = Mage::getModel('eav/entity_attribute')
00135                 ->load($data['attribute_id'])
00136                 ->setEntity(Mage::getSingleton('catalog/product')->getResource());
00137 
00138             if ($this->isUsedBySuperProducts($attribute, $data['attribute_set_id'])) {
00139                 Mage::throwException(Mage::helper('eav')->__("Attribute '%s' used in configurable products.", $attribute->getAttributeCode()));
00140             }
00141 
00142             if ($backendTable = $attribute->getBackend()->getTable()) {
00143                 $clearCondition = array(
00144                     $write->quoteInto('entity_type_id=?',$attribute->getEntityTypeId()),
00145                     $write->quoteInto('attribute_id=?',$attribute->getId()),
00146                     $write->quoteInto('entity_id IN (
00147                         SELECT entity_id FROM '.$attribute->getEntity()->getEntityTable().' WHERE attribute_set_id=?)',
00148                         $data['attribute_set_id'])
00149                 );
00150                 $write->delete($backendTable, $clearCondition);
00151             }
00152         }
00153 
00154         $write->delete($this->getTable('entity_attribute'), $condition);
00155         return $this;
00156     }

describeTable ( table  ) 

Describe table

Parameters:
string $table
Returns:
array

Definition at line 428 of file Attribute.php.

00428                                           {
00429         return $this->_getReadAdapter()->describeTable($table);
00430     }

getAttributeCodesByFrontendType ( type  ) 

Definition at line 372 of file Attribute.php.

00373     {
00374         $select = $this->_getReadAdapter()->select();
00375         $select
00376             ->from($this->getTable('eav/attribute'), 'attribute_code')
00377             ->where('frontend_input = ?', $type);
00378 
00379         $result = $this->_getReadAdapter()->fetchCol($select);
00380 
00381         if ($result) {
00382             return $result;
00383         } else {
00384             return array();
00385         }
00386     }

getFlatUpdateSelect ( Mage_Eav_Model_Entity_Attribute_Abstract attribute,
store 
)

Retrieve Select For Flat Attribute update

Parameters:
Mage_Eav_Model_Entity_Attribute_Abstract $attribute
int $store
Returns:
Varien_Db_Select

Definition at line 395 of file Attribute.php.

00395                                                                                                      {
00396         $joinCondition = "`e`.`entity_id`=`t1`.`entity_id`";
00397         if ($attribute->getFlatAddChildData()) {
00398             $joinCondition .= " AND `e`.`child_id`=`t1`.`entity_id`";
00399         }
00400         $select = $this->_getReadAdapter()->select()
00401             ->joinLeft(
00402                 array('t1' => $attribute->getBackend()->getTable()),
00403                 $joinCondition,
00404                 array()
00405                 )
00406             ->joinLeft(
00407                 array('t2' => $attribute->getBackend()->getTable()),
00408                 "t2.entity_id = t1.entity_id"
00409                     . " AND t1.entity_type_id = t2.entity_type_id"
00410                     . " AND t1.attribute_id = t2.attribute_id"
00411                     . " AND t2.store_id = {$store}",
00412                 array($attribute->getAttributeCode() => "IFNULL(t2.value, t1.value)"))
00413             ->where("t1.entity_type_id=?", $attribute->getEntityTypeId())
00414             ->where("t1.attribute_id=?", $attribute->getId())
00415             ->where("t1.store_id=?", 0);
00416         if ($attribute->getFlatAddChildData()) {
00417             $select->where("e.is_child=?", 0);
00418         }
00419         return $select;
00420     }

getIdByCode ( entityType,
code 
)

Return attribute id

Parameters:
string $entityType
string $code
Returns:
int

Definition at line 361 of file Attribute.php.

00362     {
00363         $select = $this->_getReadAdapter()->select()
00364             ->from(array('a'=>$this->getTable('eav/attribute')), array('a.attribute_id'))
00365             ->join(array('t'=>$this->getTable('eav/entity_type')), 'a.entity_type_id = t.entity_type_id', array())
00366             ->where('t.entity_type_code = ?', $entityType)
00367             ->where('a.attribute_code = ?', $code);
00368 
00369         return $this->_getReadAdapter()->fetchOne($select);
00370     }

isUsedBySuperProducts ( Mage_Core_Model_Abstract object,
attributeSet = null 
)

Definition at line 335 of file Attribute.php.

00336     {
00337         $read = $this->_getReadAdapter();
00338         $attrTable = $this->getTable('catalog/product_super_attribute');
00339         $productTable = $this->getTable('catalog/product');
00340         $select = $read->select()
00341             ->from(array('_main_table' => $attrTable), 'COUNT(*)')
00342             ->join(array('_entity'=> $productTable), '_main_table.product_id = _entity.entity_id')
00343             ->where("_main_table.attribute_id = ?", $object->getAttributeId())
00344             ->group('_main_table.attribute_id')
00345             ->limit(1);
00346 
00347         if (!is_null($attributeSet)) {
00348             $select->where('_entity.attribute_set_id = ?', $attributeSet);
00349         }
00350         $valueCount = $read->fetchOne($select);
00351         return $valueCount;
00352     }

loadByCode ( Mage_Core_Model_Abstract object,
entityTypeId,
code 
)

Enter description here...

Parameters:
Mage_Core_Model_Abstract $object
int $entityTypeId
string $code
Returns:
boolean

Definition at line 78 of file Attribute.php.

00079     {
00080         $select = $this->_getLoadSelect('attribute_code', $code, $object)
00081             ->where('entity_type_id=?', $entityTypeId);
00082         $data = $this->_getReadAdapter()->fetchRow($select);
00083 
00084         if ($data) {
00085             $object->setData($data);
00086             $this->_afterLoad($object);
00087             return true;
00088         }
00089         return false;
00090     }

saveInSetIncluding ( Mage_Core_Model_Abstract object  ) 

Enter description here...

Parameters:
Mage_Core_Model_Abstract $object
Returns:
Mage_Eav_Model_Mysql4_Entity_Attribute

Definition at line 222 of file Attribute.php.

00223     {
00224         $attrId = $object->getId();
00225         $setId  = (int) $object->getAttributeSetId();
00226         $groupId= (int) $object->getAttributeGroupId();
00227 
00228         if ($setId && $groupId && $object->getEntityTypeId()) {
00229             $write = $this->_getWriteAdapter();
00230             $table = $this->getTable('entity_attribute');
00231 
00232 
00233             $data = array(
00234                 'entity_type_id' => $object->getEntityTypeId(),
00235                 'attribute_set_id' => $setId,
00236                 'attribute_group_id' => $groupId,
00237                 'attribute_id' => $attrId,
00238                 'sort_order' => (($object->getSortOrder()) ? $object->getSortOrder() : $this->_getMaxSortOrder($object) + 1),
00239             );
00240 
00241             $condition = "$table.attribute_id = '$attrId'
00242                 AND $table.attribute_set_id = '$setId'";
00243             $write->delete($table, $condition);
00244             $write->insert($table, $data);
00245 
00246         }
00247         return $this;
00248     }


Member Data Documentation

$_entityAttributes = null [static, protected]

Definition at line 36 of file Attribute.php.


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

Generated on Sat Jul 4 17:24:12 2009 for Magento by  doxygen 1.5.8