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
00035 abstract class Mage_Eav_Model_Entity_Abstract
00036 extends Mage_Core_Model_Resource_Abstract
00037 implements Mage_Eav_Model_Entity_Interface
00038 {
00039
00040
00041
00042
00043
00044 protected $_read;
00045
00046
00047
00048
00049
00050
00051 protected $_write;
00052
00053
00054
00055
00056
00057
00058 protected $_type;
00059
00060
00061
00062
00063
00064
00065 protected $_attributesById = array();
00066
00067
00068
00069
00070
00071
00072 protected $_attributesByCode = array();
00073
00074
00075
00076
00077
00078
00079 protected $_attributesByTable = array();
00080
00081
00082
00083
00084
00085
00086 protected $_staticAttributes = array();
00087
00088
00089
00090
00091
00092
00093 protected $_entityTable;
00094
00095
00096
00097
00098
00099
00100 protected $_describeTable = array();
00101
00102
00103
00104
00105
00106
00107 protected $_entityIdField;
00108
00109
00110
00111
00112
00113
00114 protected $_valueEntityIdField;
00115
00116
00117
00118
00119
00120
00121 protected $_valueTablePrefix;
00122
00123
00124
00125
00126
00127
00128 protected $_isPartialLoad = false;
00129
00130
00131
00132
00133
00134
00135 protected $_isPartialSave = false;
00136
00137
00138
00139
00140
00141
00142 protected $_sortingSetId = null;
00143
00144
00145
00146
00147
00148
00149
00150
00151 public function setConnection(Zend_Db_Adapter_Abstract $read, Zend_Db_Adapter_Abstract $write=null)
00152 {
00153 $this->_read = $read;
00154 $this->_write = $write ? $write : $read;
00155 return $this;
00156 }
00157
00158
00159
00160
00161 protected function _construct()
00162 {
00163
00164 }
00165
00166
00167
00168
00169
00170
00171 protected function _getReadAdapter()
00172 {
00173 return $this->_read;
00174 }
00175
00176
00177
00178
00179
00180
00181 protected function _getWriteAdapter()
00182 {
00183 return $this->_write;
00184 }
00185
00186
00187
00188
00189
00190
00191 public function getReadConnection()
00192 {
00193 return $this->_getReadAdapter();
00194 }
00195
00196
00197
00198
00199
00200
00201 public function getWriteConnection()
00202 {
00203 return $this->_getWriteAdapter();
00204 }
00205
00206
00207
00208
00209
00210
00211 public function getIdFieldName()
00212 {
00213 return $this->getEntityIdField();
00214 }
00215
00216
00217
00218
00219
00220
00221
00222 public function getTable($alias)
00223 {
00224 return Mage::getSingleton('core/resource')->getTableName($alias);
00225 }
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235 public function setType($type)
00236 {
00237 $this->_type = Mage::getSingleton('eav/config')->getEntityType($type);
00238 $this->_afterSetConfig();
00239 return $this;
00240 }
00241
00242
00243
00244
00245
00246
00247 public function getEntityType()
00248 {
00249 if (empty($this->_type)) {
00250 throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Entity is not initialized'));
00251 }
00252 return $this->_type;
00253 }
00254
00255
00256
00257
00258
00259
00260 public function getType()
00261 {
00262 return $this->getEntityType()->getEntityTypeCode();
00263 }
00264
00265
00266
00267
00268
00269
00270 public function getTypeId()
00271 {
00272 return (int)$this->getEntityType()->getEntityTypeId();
00273 }
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284 public function unsetAttributes($attributes=null)
00285 {
00286 if (is_null($attributes)) {
00287 $this->_attributesByCode = array();
00288 $this->_attributesById = array();
00289 $this->_attributesByTable = array();
00290 return $this;
00291 }
00292
00293 if (is_string($attributes)) {
00294 $attributes = array($attributes);
00295 }
00296
00297 if (!is_array($attributes)) {
00298 throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Unknown parameter'));
00299 }
00300
00301 foreach ($attributes as $attrCode) {
00302 if (!isset($this->_attributesByCode[$attrCode])) {
00303 continue;
00304 }
00305
00306 $attr = $this->getAttribute($attrCode);
00307 unset($this->_attributesById[$attr->getId()]);
00308 unset($this->_attributesByTable[$attr->getBackend()->getTable()][$attrCode]);
00309 unset($this->_attributesByCode[$attrCode]);
00310 }
00311
00312 return $this;
00313 }
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325 public function getAttribute($attribute)
00326 {
00327 if (is_numeric($attribute)) {
00328 $attributeId = $attribute;
00329
00330 if (isset($this->_attributesById[$attributeId])) {
00331 return $this->_attributesById[$attributeId];
00332 }
00333 $attributeInstance = Mage::getSingleton('eav/config')->getAttribute($this->getEntityType(), $attributeId);
00334 if ($attributeInstance) {
00335 $attributeCode = $attributeInstance->getAttributeCode();
00336 }
00337
00338 } elseif (is_string($attribute)) {
00339 $attributeCode = $attribute;
00340
00341 if (isset($this->_attributesByCode[$attributeCode])) {
00342 return $this->_attributesByCode[$attributeCode];
00343 }
00344 $attributeInstance = Mage::getSingleton('eav/config')
00345 ->getAttribute($this->getEntityType(), $attributeCode);
00346 if (!$attributeInstance->getAttributeCode() && in_array($attribute, $this->getDefaultAttributes())) {
00347 $attributeInstance
00348 ->setAttributeCode($attribute)
00349 ->setBackendType(Mage_Eav_Model_Entity_Attribute_Abstract::TYPE_STATIC)
00350 ->setIsGlobal(1)
00351 ->setEntity($this)
00352 ->setEntityType($this->getEntityType())
00353 ->setEntityTypeId($this->getEntityType()->getId());
00354 }
00355 } elseif ($attribute instanceof Mage_Eav_Model_Entity_Attribute_Abstract) {
00356
00357 $attributeInstance = $attribute;
00358 $attributeCode = $attributeInstance->getAttributeCode();
00359 if (isset($this->_attributesByCode[$attributeCode])) {
00360 return $this->_attributesByCode[$attributeCode];
00361 }
00362 }
00363
00364 if (empty($attributeInstance)
00365 || !($attributeInstance instanceof Mage_Eav_Model_Entity_Attribute_Abstract)
00366 || (!$attributeInstance->getId() && !in_array($attributeInstance->getAttributeCode(), $this->getDefaultAttributes()))
00367 ) {
00368 return false;
00369 }
00370
00371 $attribute = $attributeInstance;
00372
00373 if (empty($attributeId)) {
00374 $attributeId = $attribute->getAttributeId();
00375 }
00376
00377 if (!$attribute->getAttributeCode()) {
00378 $attribute->setAttributeCode($attributeCode);
00379 }
00380 if (!$attribute->getAttributeModel()) {
00381 $attribute->setAttributeModel($this->_getDefaultAttributeModel());
00382 }
00383
00384 $this->addAttribute($attribute);
00385
00386 return $attribute;
00387 }
00388
00389
00390
00391
00392
00393
00394
00395 public function addAttribute(Mage_Eav_Model_Entity_Attribute_Abstract $attribute)
00396 {
00397 $attribute->setEntity($this);
00398 $attributeCode = $attribute->getAttributeCode();
00399
00400 $this->_attributesByCode[$attributeCode] = $attribute;
00401
00402 if ($attribute->isStatic()) {
00403 $this->_staticAttributes[$attributeCode] = $attribute;
00404 } else {
00405 $this->_attributesById[$attribute->getId()] = $attribute;
00406 $this->_attributesByTable[$attribute->getBackendTable()][$attributeCode] = $attribute;
00407 }
00408 return $this;
00409 }
00410
00411
00412
00413
00414
00415
00416
00417 public function isPartialLoad($flag=null)
00418 {
00419 $result = $this->_isPartialLoad;
00420 if (!is_null($flag)) {
00421 $this->_isPartialLoad = $flag;
00422 }
00423 return $result;
00424 }
00425
00426
00427
00428
00429
00430
00431
00432 public function isPartialSave($flag=null)
00433 {
00434 $result = $this->_isPartialSave;
00435 if (!is_null($flag)) {
00436 $this->_isPartialSave = $flag;
00437 }
00438 return $result;
00439 }
00440
00441
00442
00443
00444
00445
00446 public function loadAllAttributes($object=null)
00447 {
00448 $attributeCodes = Mage::getSingleton('eav/config')
00449 ->getEntityAttributeCodes($this->getEntityType(), $object);
00450
00451
00452
00453
00454 $defaultAttributes = $this->getDefaultAttributes();
00455 foreach ($defaultAttributes as $attributeCode) {
00456 $attributeIndex = array_search($attributeCode, $attributeCodes);
00457 if ($attributeIndex !== false) {
00458 $this->getAttribute($attributeCodes[$attributeIndex]);
00459 unset($attributeCodes[$attributeIndex]);
00460 } else {
00461 $attribute = Mage::getModel($this->getEntityType()->getAttributeModel());
00462 $attribute->setAttributeCode($attributeCode)
00463 ->setBackendType('static')
00464 ->setEntityType($this->getEntityType())
00465 ->setEntityTypeId($this->getEntityType()->getId());
00466 $this->addAttribute($attribute);
00467 }
00468 }
00469
00470 foreach ($attributeCodes as $code) {
00471 $this->getAttribute($code);
00472 }
00473
00474 return $this;
00475 }
00476
00477
00478
00479
00480
00481
00482
00483 public function getSortedAttributes($setId = null)
00484 {
00485 $attributes = $this->getAttributesByCode();
00486 if (is_null($setId)) {
00487 $setId = $this->getEntityType()->getDefaultAttributeSetId();
00488 }
00489
00490
00491 Mage::getSingleton('eav/entity_attribute_set')
00492 ->addSetInfo($this->getEntityType(), $attributes, $setId);
00493
00494 foreach ($attributes as $code => $attribute) {
00495
00496 if (!$attribute->isInSet($setId)) {
00497 unset($attributes[$code]);
00498 }
00499 }
00500
00501 $this->_sortingSetId = $setId;
00502 uasort($attributes, array($this, 'attributesCompare'));
00503 return $attributes;
00504 }
00505
00506 public function attributesCompare($attribute1, $attribute2)
00507 {
00508 $sortPath = 'attribute_set_info/' . $this->_sortingSetId . '/sort';
00509 $groupSortPath = 'attribute_set_info/' . $this->_sortingSetId . '/group_sort';
00510
00511 $sort1 = ($attribute1->getData($groupSortPath) * 1000) + ($attribute1->getData($sortPath) * 0.0001);
00512 $sort2 = ($attribute2->getData($groupSortPath) * 1000) + ($attribute2->getData($sortPath) * 0.0001);
00513
00514 if ($sort1 > $sort2) {
00515 return 1;
00516 } elseif ($sort1 < $sort2) {
00517 return -1;
00518 }
00519
00520 return 0;
00521 }
00522
00523
00524
00525
00526
00527
00528
00529
00530 protected function _isApplicableAttribute($object, $attribute)
00531 {
00532 return true;
00533 }
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548 public function walkAttributes($partMethod, array $args=array())
00549 {
00550 $methodArr = explode('/', $partMethod);
00551 switch (sizeof($methodArr)) {
00552 case 1:
00553 $part = 'attribute';
00554 $method = $methodArr[0];
00555 break;
00556
00557 case 2:
00558 $part = $methodArr[0];
00559 $method = $methodArr[1];
00560 break;
00561 }
00562 $results = array();
00563 foreach ($this->getAttributesByCode() as $attrCode=>$attribute) {
00564
00565 if (isset($args[0]) && is_object($args[0]) && !$this->_isApplicableAttribute($args[0], $attribute)) {
00566 continue;
00567 }
00568
00569 switch ($part) {
00570 case 'attribute':
00571 $instance = $attribute;
00572 break;
00573
00574 case 'backend':
00575 $instance = $attribute->getBackend();
00576 break;
00577
00578 case 'frontend':
00579 $instance = $attribute->getFrontend();
00580 break;
00581
00582 case 'source':
00583 $instance = $attribute->getSource();
00584 break;
00585 }
00586
00587 try {
00588 $results[$attrCode] = call_user_func_array(array($instance, $method), $args);
00589 }
00590 catch (Mage_Eav_Model_Entity_Attribute_Exception $e) {
00591 throw $e;
00592 }
00593 catch (Exception $e) {
00594 $exception = new Mage_Eav_Model_Entity_Attribute_Exception($e->getMessage());
00595 $exception->setAttributeCode($attrCode)->setPart($part);
00596 throw $exception;
00597 }
00598 }
00599 return $results;
00600 }
00601
00602
00603
00604
00605
00606
00607 public function getAttributesByCode()
00608 {
00609 return $this->_attributesByCode;
00610 }
00611
00612
00613
00614
00615
00616
00617 public function getAttributesById()
00618 {
00619 return $this->_attributesById;
00620 }
00621
00622
00623
00624
00625
00626
00627 public function getAttributesByTable()
00628 {
00629 return $this->_attributesByTable;
00630 }
00631
00632
00633
00634
00635
00636
00637 public function getEntityTable()
00638 {
00639 if (empty($this->_entityTable)) {
00640 $table = $this->getEntityType()->getEntityTable();
00641 if (empty($table)) {
00642 $table = Mage_Eav_Model_Entity::DEFAULT_ENTITY_TABLE;
00643 }
00644 $this->_entityTable = Mage::getSingleton('core/resource')->getTableName($table);
00645 }
00646 return $this->_entityTable;
00647 }
00648
00649
00650
00651
00652
00653
00654 public function getEntityIdField()
00655 {
00656 if (empty($this->_entityIdField)) {
00657 $this->_entityIdField = $this->getEntityType()->getEntityIdField();
00658 if (empty($this->_entityIdField)) {
00659 $this->_entityIdField = Mage_Eav_Model_Entity::DEFAULT_ENTITY_ID_FIELD;
00660 }
00661 }
00662 return $this->_entityIdField;
00663 }
00664
00665
00666
00667
00668
00669
00670 public function getValueEntityIdField()
00671 {
00672 return $this->getEntityIdField();
00673 }
00674
00675
00676
00677
00678
00679
00680 public function getValueTablePrefix()
00681 {
00682 if (empty($this->_valueTablePrefix)) {
00683 $prefix = (string)$this->getEntityType()->getValueTablePrefix();
00684 if (!empty($prefix)) {
00685 $this->_valueTablePrefix = $prefix;
00686
00687
00688
00689
00690 } else {
00691 $this->_valueTablePrefix = $this->getEntityTable();
00692 }
00693 }
00694 return $this->_valueTablePrefix;
00695 }
00696
00697
00698
00699
00700
00701
00702
00703
00704 public function isAttributeStatic($attribute)
00705 {
00706 $attrInstance = $this->getAttribute($attribute);
00707 return $attrInstance && $attrInstance->getBackend()->isStatic();
00708 }
00709
00710
00711
00712
00713
00714
00715
00716 public function validate($object)
00717 {
00718 $this->loadAllAttributes($object);
00719 $this->walkAttributes('backend/validate', array($object));
00720
00721 return $this;
00722 }
00723
00724
00725
00726
00727
00728
00729
00730 public function setNewIncrementId(Varien_Object $object)
00731 {
00732 if ($object->getIncrementId()) {
00733 return $this;
00734 }
00735
00736 $incrementId = $this->getEntityType()->fetchNewIncrementId($object->getStoreId());
00737
00738 if (false!==$incrementId) {
00739 $object->setIncrementId($incrementId);
00740 }
00741
00742 return $this;
00743 }
00744
00745
00746
00747
00748
00749
00750
00751
00752 public function checkAttributeUniqueValue(Mage_Eav_Model_Entity_Attribute_Abstract $attribute, $object)
00753 {
00754 if ($attribute->getBackend()->getType()==='static') {
00755 $select = $this->_getWriteAdapter()->select()
00756 ->from($this->getEntityTable(), $this->getEntityIdField())
00757 ->where('entity_type_id=?', $this->getTypeId())
00758 ->where($attribute->getAttributeCode().'=?', $object->getData($attribute->getAttributeCode()));
00759 } else {
00760 $select = $this->_getWriteAdapter()->select()
00761 ->from($attribute->getBackend()->getTable(), $attribute->getBackend()->getEntityIdField())
00762 ->where('entity_type_id=?', $this->getTypeId())
00763 ->where('attribute_id=?', $attribute->getId())
00764 ->where('value=?', $object->getData($attribute->getAttributeCode()));
00765 }
00766 $data = $this->_getWriteAdapter()->fetchCol($select);
00767
00768 if ($object->getId()) {
00769 if (isset($data[0])) {
00770 return $data[0] == $object->getId();
00771 }
00772 return true;
00773 }
00774 else {
00775 return !count($data);
00776 }
00777 }
00778
00779
00780
00781
00782
00783
00784 public function getDefaultAttributeSourceModel()
00785 {
00786 return Mage_Eav_Model_Entity::DEFAULT_SOURCE_MODEL;
00787 }
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797 public function load($object, $entityId, $attributes=array())
00798 {
00799 Varien_Profiler::start('__EAV_LOAD_MODEL__');
00800
00801
00802
00803 $select = $this->_getLoadRowSelect($object, $entityId);
00804 $row = $this->_getReadAdapter()->fetchRow($select);
00805
00806 if (is_array($row)) {
00807 $object->addData($row);
00808 }
00809
00810 if (empty($attributes)) {
00811 $this->loadAllAttributes($object);
00812 } else {
00813 foreach ($attributes as $attrCode) {
00814 $this->getAttribute($attrCode);
00815 }
00816 }
00817
00818
00819
00820
00821 Varien_Profiler::start('__EAV_LOAD_MODEL_ATTRIBUTES__');
00822 foreach ($this->getAttributesByTable() as $table=>$attributes) {
00823 $select = $this->_getLoadAttributesSelect($object, $table);
00824 $values = $this->_getReadAdapter()->fetchAll($select);
00825
00826 foreach ($values as $valueRow) {
00827 $this->_setAttribteValue($object, $valueRow);
00828 }
00829 }
00830 Varien_Profiler::stop('__EAV_LOAD_MODEL_ATTRIBUTES__');
00831
00832 $object->setOrigData();
00833 Varien_Profiler::start('__EAV_LOAD_MODEL_AFTER_LOAD__');
00834 $this->_afterLoad($object);
00835 Varien_Profiler::stop('__EAV_LOAD_MODEL_AFTER_LOAD__');
00836
00837 Varien_Profiler::stop('__EAV_LOAD_MODEL__');
00838 return $this;
00839 }
00840
00841
00842
00843
00844
00845
00846
00847
00848 protected function _getLoadRowSelect($object, $rowId)
00849 {
00850 $select = $this->_read->select()
00851 ->from($this->getEntityTable())
00852 ->where($this->getEntityIdField()."=?", $rowId);
00853
00854 return $select;
00855 }
00856
00857
00858
00859
00860
00861
00862
00863
00864 protected function _getLoadAttributesSelect($object, $table)
00865 {
00866 $select = $this->_read->select()
00867 ->from($table)
00868 ->where($this->getEntityIdField() . '=?', $object->getId());
00869 return $select;
00870 }
00871
00872
00873
00874
00875
00876
00877
00878
00879 protected function _setAttribteValue($object, $valueRow)
00880 {
00881 if ($attribute = $this->getAttribute($valueRow['attribute_id'])) {
00882 $attributeCode = $attribute->getAttributeCode();
00883 $object->setData($attributeCode, $valueRow['value']);
00884 $attribute->getBackend()->setValueId($valueRow['value_id']);
00885 }
00886 return $this;
00887 }
00888
00889
00890
00891
00892
00893
00894
00895 public function save(Varien_Object $object)
00896 {
00897 if ($object->isDeleted()) {
00898 return $this->delete($object);
00899 }
00900
00901 if (!$this->isPartialSave()) {
00902 $this->loadAllAttributes($object);
00903 }
00904
00905 if (!$object->getEntityTypeId()) {
00906 $object->setEntityTypeId($this->getTypeId());
00907 }
00908
00909 $object->setParentId((int) $object->getParentId());
00910
00911 $this->_beforeSave($object);
00912 $this->_processSaveData($this->_collectSaveData($object));
00913 $this->_afterSave($object);
00914
00915 return $this;
00916 }
00917
00918 protected function _getOrigObject($object)
00919 {
00920 $className = get_class($object);
00921 $origObject = new $className();
00922 $origObject->setData(array());
00923 $this->load($origObject, $object->getData($this->getEntityIdField()));
00924 return $origObject;
00925 }
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937
00938 protected function _collectSaveData($newObject)
00939 {
00940 $newData = $newObject->getData();
00941 $entityId = $newObject->getData($this->getEntityIdField());
00942 if (!empty($entityId)) {
00943
00944
00945
00946
00947
00948
00949
00950
00951 $origData = $this->_getOrigObject($newObject)->getOrigData();
00952
00953
00954
00955
00956
00957 foreach ($origData as $k=>$v) {
00958 if (!array_key_exists($k, $newData)) {
00959 unset($origData[$k]);
00960 }
00961 }
00962 }
00963
00964 foreach ($newData as $k=>$v) {
00965
00966
00967
00968 if (is_numeric($k) || is_array($v)) {
00969 continue;
00970 throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Invalid data object key'));
00971 }
00972
00973 $attribute = $this->getAttribute($k);
00974 if (empty($attribute)) {
00975 continue;
00976 }
00977
00978 $attrId = $attribute->getAttributeId();
00979
00980
00981
00982
00983 if ($this->isAttributeStatic($k)) {
00984 $entityRow[$k] = $this->_prepareStaticValue($k, $v);
00985 continue;
00986 }
00987
00988
00989
00990
00991 if (isset($origData[$k])) {
00992 if ($attribute->isValueEmpty($v)) {
00993 $delete[$attribute->getBackend()->getTable()][] = array(
00994 'attribute_id' => $attrId,
00995 'value_id' => $attribute->getBackend()->getValueId()
00996 );
00997 }
00998 elseif ($v!==$origData[$k]) {
00999 $update[$attrId] = array(
01000 'value_id' => $attribute->getBackend()->getValueId(),
01001 'value' => $v,
01002 );
01003 }
01004 }
01005 elseif (!$attribute->isValueEmpty($v)) {
01006 $insert[$attrId] = $v;
01007 }
01008 }
01009
01010 $result = compact('newObject', 'entityRow', 'insert', 'update', 'delete');
01011 return $result;
01012 }
01013
01014
01015
01016
01017
01018
01019
01020 protected function _getStaticFieldProperties($field)
01021 {
01022 if (empty($this->_describeTable[$this->getEntityTable()])) {
01023 $this->_describeTable[$this->getEntityTable()] = $this->_getWriteAdapter()->describeTable($this->getEntityTable());
01024 }
01025
01026 if (isset($this->_describeTable[$this->getEntityTable()][$field])) {
01027 return $this->_describeTable[$this->getEntityTable()][$field];
01028 }
01029
01030 return false;
01031 }
01032
01033
01034
01035
01036
01037
01038
01039
01040 protected function _prepareStaticValue($key, $value)
01041 {
01042 $fieldProp = $this->_getStaticFieldProperties($key);
01043
01044 if (!$fieldProp) {
01045 return $value;
01046 }
01047
01048 if ($fieldProp['DATA_TYPE'] == 'decimal') {
01049 $value = Mage::app()->getLocale()->getNumber($value);
01050 }
01051
01052 return $value;
01053 }
01054
01055
01056
01057
01058
01059
01060
01061 protected function _processSaveData($saveData)
01062 {
01063 extract($saveData);
01064 $insertEntity = true;
01065 $entityIdField = $this->getEntityIdField();
01066 $entityId = $newObject->getId();
01067 $condition = $this->_getWriteAdapter()->quoteInto("$entityIdField=?", $entityId);
01068
01069 if (!empty($entityId)) {
01070 $select = $this->_getWriteAdapter()->select()
01071 ->from($this->getEntityTable(), $entityIdField)
01072 ->where($condition);
01073 if ($this->_getWriteAdapter()->fetchOne($select)) {
01074 $insertEntity = false;
01075 }
01076 }
01077
01078
01079
01080
01081 if ($insertEntity) {
01082 $this->_getWriteAdapter()->insert($this->getEntityTable(), $entityRow);
01083 $entityId = $this->_getWriteAdapter()->lastInsertId();
01084 $newObject->setId($entityId);
01085 } else {
01086 $this->_getWriteAdapter()->update($this->getEntityTable(), $entityRow, $condition);
01087 }
01088
01089
01090
01091
01092 if (!empty($insert)) {
01093 foreach ($insert as $attrId=>$value) {
01094 $attribute = $this->getAttribute($attrId);
01095 $this->_insertAttribute($newObject, $attribute, $value);
01096 }
01097 }
01098
01099
01100
01101
01102 if (!empty($update)) {
01103 foreach ($update as $attrId=>$v) {
01104 $attribute = $this->getAttribute($attrId);
01105 $this->_updateAttribute($newObject, $attribute, $v['value_id'], $v['value']);
01106 }
01107 }
01108
01109
01110
01111
01112 if (!empty($delete)) {
01113 foreach ($delete as $table=>$values) {
01114 $this->_deleteAttributes($newObject, $table, $values);
01115 }
01116 }
01117
01118 return $this;
01119 }
01120
01121
01122
01123
01124
01125
01126
01127
01128
01129 protected function _insertAttribute($object, $attribute, $value)
01130 {
01131 $entityIdField = $attribute->getBackend()->getEntityIdField();
01132 $row = array(
01133 $entityIdField => $object->getId(),
01134 'entity_type_id'=> $object->getEntityTypeId(),
01135 'attribute_id' => $attribute->getId(),
01136 'value' => $this->_prepareValueForSave($value, $attribute),
01137 );
01138 $this->_getWriteAdapter()->insert($attribute->getBackend()->getTable(), $row);
01139 return $this;
01140 }
01141
01142
01143
01144
01145
01146
01147
01148
01149
01150
01151 protected function _updateAttribute($object, $attribute, $valueId, $value)
01152 {
01153 $this->_getWriteAdapter()->update($attribute->getBackend()->getTable(),
01154 array('value' => $this->_prepareValueForSave($value, $attribute)),
01155 'value_id='.(int)$valueId
01156 );
01157 return $this;
01158 }
01159
01160
01161
01162
01163
01164
01165
01166
01167 protected function _prepareValueForSave($value, Mage_Eav_Model_Entity_Attribute_Abstract $attribute)
01168 {
01169 if ($attribute->getBackendType() == 'decimal') {
01170 return Mage::app()->getLocale()->getNumber($value);
01171 }
01172 return $value;
01173 }
01174
01175
01176
01177
01178
01179
01180
01181
01182
01183 protected function _deleteAttributes($object, $table, $info)
01184 {
01185 $valueIds = array();
01186 foreach ($info as $itemData) {
01187 $valueIds[] = $itemData['value_id'];
01188 }
01189 if (!empty($valueIds)) {
01190 $condition = $this->_getWriteAdapter()->quoteInto('value_id IN (?)', $valueIds);
01191 $this->_getWriteAdapter()->delete($table, $condition);
01192 }
01193 return $this;
01194 }
01195
01196
01197
01198
01199
01200
01201
01202
01203 public function saveAttribute(Varien_Object $object, $attributeCode)
01204 {
01205 $attribute = $this->getAttribute($attributeCode);
01206 $backend = $attribute->getBackend();
01207 $table = $backend->getTable();
01208 $entity = $attribute->getEntity();
01209 $entityIdField = $entity->getEntityIdField();
01210
01211 $row = array(
01212 'entity_type_id' => $entity->getTypeId(),
01213 'attribute_id' => $attribute->getId(),
01214 $entityIdField=> $object->getData($entityIdField),
01215 );
01216
01217 $newValue = $object->getData($attributeCode);
01218 if ($attribute->isValueEmpty($newValue)) {
01219 $newValue = null;
01220 }
01221
01222 $whereArr = array();
01223 foreach ($row as $field => $value) {
01224 $whereArr[] = $this->_read->quoteInto("$field=?", $value);
01225 }
01226 $where = '('.join(') AND (', $whereArr).')';
01227
01228 $this->_getWriteAdapter()->beginTransaction();
01229
01230 try {
01231 $select = $this->_getWriteAdapter()->select()
01232 ->from($table, 'value_id')
01233 ->where($where);
01234 $origValueId = $this->_getWriteAdapter()->fetchOne($select);
01235
01236 if ($origValueId === false && !is_null($newValue)) {
01237 $this->_insertAttribute($object, $attribute, $newValue);
01238 $backend->setValueId($this->_getWriteAdapter()->lastInsertId());
01239 } elseif ($origValueId !== false && !is_null($newValue)) {
01240 $this->_updateAttribute($object, $attribute, $origValueId, $newValue);
01241 } elseif ($origValueId !== false && is_null($newValue)) {
01242 $this->_getWriteAdapter()->delete($table, $where);
01243 }
01244 $this->_getWriteAdapter()->commit();
01245 } catch (Exception $e) {
01246 $this->_getWriteAdapter()->rollback();
01247 throw $e;
01248 }
01249
01250 return $this;
01251 }
01252
01253
01254
01255
01256
01257
01258 public function delete($object)
01259 {
01260 if (is_numeric($object)) {
01261 $id = (int)$object;
01262 } elseif ($object instanceof Varien_Object) {
01263 $id = (int)$object->getId();
01264 }
01265
01266 $this->_beforeDelete($object);
01267
01268 try {
01269 $this->_getWriteAdapter()->delete($this->getEntityTable(), $this->getEntityIdField()."=".$id);
01270 $this->loadAllAttributes($object);
01271 foreach ($this->getAttributesByTable() as $table=>$attributes) {
01272 $this->_getWriteAdapter()->delete($table, $this->getEntityIdField()."=".$id);
01273 }
01274 } catch (Exception $e) {
01275 throw $e;
01276 }
01277
01278 $this->_afterDelete($object);
01279 return $this;
01280 }
01281
01282
01283
01284
01285
01286
01287 protected function _afterLoad(Varien_Object $object)
01288 {
01289 $this->walkAttributes('backend/afterLoad', array($object));
01290 }
01291
01292
01293
01294
01295
01296
01297 protected function _beforeSave(Varien_Object $object)
01298 {
01299 $this->walkAttributes('backend/beforeSave', array($object));
01300 }
01301
01302
01303
01304
01305
01306
01307 protected function _afterSave(Varien_Object $object)
01308 {
01309 $this->walkAttributes('backend/afterSave', array($object));
01310 }
01311
01312
01313
01314
01315
01316
01317 protected function _beforeDelete(Varien_Object $object)
01318 {
01319 $this->walkAttributes('backend/beforeDelete', array($object));
01320 }
01321
01322
01323
01324
01325
01326
01327 protected function _afterDelete(Varien_Object $object)
01328 {
01329 $this->walkAttributes('backend/afterDelete', array($object));
01330 }
01331
01332
01333
01334
01335
01336
01337 protected function _getDefaultAttributeModel()
01338 {
01339 return Mage_Eav_Model_Entity::DEFAULT_ATTRIBUTE_MODEL;
01340 }
01341
01342
01343
01344
01345
01346
01347 protected function _getDefaultAttributes()
01348 {
01349 return array('entity_type_id', 'attribute_set_id', 'created_at', 'updated_at', 'parent_id', 'increment_id');
01350 }
01351
01352
01353
01354
01355
01356
01357 public function getDefaultAttributes() {
01358 return array_unique(array_merge($this->_getDefaultAttributes(), array($this->getEntityIdField())));
01359 }
01360
01361
01362
01363
01364
01365
01366
01367 protected function _afterSetConfig()
01368 {
01369 return $this;
01370
01371
01372
01373
01374
01375
01376
01377
01378
01379
01380
01381
01382
01383
01384
01385
01386
01387
01388 }
01389 }