Skip to content

Commit 7f156da

Browse files
author
Maksym Savich
committed
MAGETWO-35462: Refactor \Magento\Framework\Model\Resource\Db\AbstractDb only update changed fields
1 parent 082c063 commit 7f156da

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

lib/internal/Magento/Framework/Model/AbstractModel.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,4 +714,14 @@ protected function updateStoredData()
714714
}
715715
return $this;
716716
}
717+
718+
/**
719+
* Model StoredData getter
720+
*
721+
* @return array
722+
*/
723+
public function getStoredData()
724+
{
725+
return $this->storedData;
726+
}
717727
}

lib/internal/Magento/Framework/Model/Resource/Db/AbstractDb.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,10 @@ public function save(\Magento\Framework\Model\AbstractModel $object)
419419
if ($this->_isPkAutoIncrement) {
420420
$data = $this->_prepareDataForSave($object);
421421
unset($data[$this->getIdFieldName()]);
422-
$this->_getWriteAdapter()->update($this->getMainTable(), $data, $condition);
422+
$data = $this->prepareDataForUpdate($data, $object->getStoredData());
423+
if (!empty($data)) {
424+
$this->_getWriteAdapter()->update($this->getMainTable(), $data, $condition);
425+
}
423426
} else {
424427
$select = $this->_getWriteAdapter()->select()->from(
425428
$this->getMainTable(),
@@ -430,6 +433,7 @@ public function save(\Magento\Framework\Model\AbstractModel $object)
430433
if ($this->_getWriteAdapter()->fetchOne($select) !== false) {
431434
$data = $this->_prepareDataForSave($object);
432435
unset($data[$this->getIdFieldName()]);
436+
$data = $this->prepareDataForUpdate($data, $object->getStoredData());
433437
if (!empty($data)) {
434438
$this->_getWriteAdapter()->update($this->getMainTable(), $data, $condition);
435439
}
@@ -770,4 +774,21 @@ public function getChecksum($table)
770774
}
771775
return $checksum;
772776
}
777+
778+
/**
779+
* Get the array of data fields that was changed or added
780+
*
781+
* @param array $data
782+
* @param array $storedData
783+
* @return array
784+
*/
785+
protected function prepareDataForUpdate($data, $storedData)
786+
{
787+
foreach ($storedData as $key => $value) {
788+
if(array_key_exists($key, $data) && $data[$key] == $value) {
789+
unset($data[$key]);
790+
}
791+
}
792+
return $data;
793+
}
773794
}

0 commit comments

Comments
 (0)