在magento的eav模型中如何在更新记录时只在value表的原值上更新
1,一般情况下,当我们在调用getModel在load某条实体接着更新对应实体上的值是,都不会覆盖原来的实体value表上的值,而是保留原来的,并在value表上重新创建一条值记录,比如初始表如下:
当我们更新属性值时,如更新entity_id为19的值,表变化如下:
值表并没有更新值,而是重新新建一条记录
2,针对以上问题,我们在model的Resource类重写_updateAttribute()方法,代码如下:
1 protected function _updateAttribute($object, $attribute, $valueId, $value) 2 { 3 $table = $attribute->getBackend()->getTable(); 4 if (!isset($this->_attributeValuesToSave[$table])) { 5 $this->_attributeValuesToSave[$table] = array(); 6 } 7 8 $entityIdField = $attribute->getBackend()->getEntityIdField(); 9 10 $data = array( 11 'entity_type_id' => $object->getEntityTypeId(), 12 $entityIdField => $object->getId(), 13 'attribute_id' => $attribute->getId(), 14 'value' => $this->_prepareValueForSave($value, $attribute) 15 ); 16 if ($valueId) 17 { 18 $data['value_id'] = $valueId; 19 } 20 21 $this->_attributeValuesToSave[$table][] = $data; 22 23 return $this; 24 }
接着对entity_id=18的实体进行更新值,可以覆盖值,表变化如下:
总结:对于magento,数据更新值为什么不按正常的思路走这也是让我困惑的地方?就先记录着,以后有时间细细琢磨...