@@ -1592,22 +1592,12 @@ protected function _processAttributeValues()
1592
1592
foreach ($ this ->_attributeValuesToSave as $ table => $ data ) {
1593
1593
$ insert = [];
1594
1594
foreach ($ data as $ attributeData ) {
1595
- $ select = $ connection ->select ()->from ($ table , 'value_id ' )->where ("entity_id = :entity_id AND attribute_id = :attribute_id " );
1596
- $ result = $ connection ->fetchOne ($ select , [
1597
- 'attribute_id ' => $ attributeData ['attribute_id ' ],
1598
- 'entity_id ' => $ attributeData ['entity_id ' ]
1599
- ]);
1600
- if ($ result ) {
1601
- $ updateWhere = [];
1602
- $ updateWhere [] = sprintf ('%s=%d ' , $ connection ->quoteIdentifier ('entity_id ' ), $ attributeData ['entity_id ' ]);
1603
- $ updateWhere [] = sprintf ('%s=%d ' , $ connection ->quoteIdentifier ('attribute_id ' ), $ attributeData ['attribute_id ' ]);
1604
- $ connection ->update ($ table , ['value ' => $ attributeData ['value ' ]], $ updateWhere );
1595
+ $ whereValues = $ attributeData ;
1596
+ unset($ whereValues ['value ' ]);
1597
+ if ($ valueId = $ this ->checkExistingAttributeValue ($ table , $ attributeData )) {
1598
+ $ connection ->update ($ table , ['value ' => $ attributeData ['value ' ]], $ valueId );
1605
1599
} else {
1606
- $ insert [] = [
1607
- 'attribute_id ' => $ attributeData ['attribute_id ' ],
1608
- 'entity_id ' => $ attributeData ['entity_id ' ],
1609
- 'value ' => $ attributeData ['value ' ]
1610
- ];
1600
+ $ insert [] = $ attributeData ;
1611
1601
}
1612
1602
}
1613
1603
@@ -1627,6 +1617,27 @@ protected function _processAttributeValues()
1627
1617
return $ this ;
1628
1618
}
1629
1619
1620
+ /**
1621
+ * Checks for existing attribute record
1622
+ *
1623
+ * @param string $table
1624
+ * @param array $attributeData
1625
+ * @return string
1626
+ */
1627
+ protected function checkExistingAttributeValue (string $ table , array $ attributeData ): string
1628
+ {
1629
+ $ connection = $ this ->getConnection ();
1630
+ $ where = [];
1631
+ unset($ attributeData ['value ' ]);
1632
+
1633
+ foreach ($ attributeData as $ key => $ val ) {
1634
+ $ where [] = sprintf ('%s = :%s ' , $ key , $ key );
1635
+ }
1636
+ $ select = $ connection ->select ()->from ($ table , 'value_id ' )->where (implode (' AND ' , $ where ));
1637
+
1638
+ return $ connection ->fetchOne ($ select , $ attributeData );
1639
+ }
1640
+
1630
1641
/**
1631
1642
* Prepare value for save
1632
1643
*
0 commit comments