Skip to content

Commit 619ab67

Browse files
MC-30544: "updated_at" timestamp not updating for products updated via "Update Attribute" action
1 parent 06a0dd0 commit 619ab67

File tree

2 files changed

+61
-12
lines changed

2 files changed

+61
-12
lines changed

app/code/Magento/Catalog/Model/ResourceModel/Product/Action.php

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Catalog\Model\ResourceModel\Product;
77

8+
use Magento\Catalog\Api\Data\ProductInterface;
89
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
910

1011
/**
@@ -14,6 +15,32 @@
1415
*/
1516
class Action extends \Magento\Catalog\Model\ResourceModel\AbstractResource
1617
{
18+
/**
19+
* @var \Magento\Framework\Stdlib\DateTime\DateTime
20+
*/
21+
private $dateTime;
22+
23+
/**
24+
* @param \Magento\Eav\Model\Entity\Context $context
25+
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
26+
* @param \Magento\Catalog\Model\Factory $modelFactory
27+
* @param \Magento\Eav\Model\Entity\Attribute\UniqueValidationInterface $uniqueValidator
28+
* @param \Magento\Framework\Stdlib\DateTime\DateTime $dateTime
29+
* @param array $data
30+
*/
31+
public function __construct(
32+
\Magento\Eav\Model\Entity\Context $context,
33+
\Magento\Store\Model\StoreManagerInterface $storeManager,
34+
\Magento\Catalog\Model\Factory $modelFactory,
35+
\Magento\Eav\Model\Entity\Attribute\UniqueValidationInterface $uniqueValidator,
36+
\Magento\Framework\Stdlib\DateTime\DateTime $dateTime,
37+
$data = []
38+
) {
39+
parent::__construct($context, $storeManager, $modelFactory, $data, $uniqueValidator);
40+
41+
$this->dateTime = $dateTime;
42+
}
43+
1744
/**
1845
* Initialize connection
1946
*
@@ -43,6 +70,7 @@ public function updateAttributes($entityIds, $attrData, $storeId)
4370
$object = new \Magento\Framework\DataObject();
4471
$object->setStoreId($storeId);
4572

73+
$attrData[ProductInterface::UPDATED_AT] = $this->dateTime->gmtDate();
4674
$this->getConnection()->beginTransaction();
4775
try {
4876
foreach ($attrData as $attrCode => $value) {
@@ -95,7 +123,7 @@ protected function _saveAttributeValue($object, $attribute, $value)
95123
* for default store id
96124
* In this case we clear all not default values
97125
*/
98-
if ($this->_storeManager->hasSingleStore()) {
126+
if ($this->_storeManager->hasSingleStore() && !$attribute->isStatic()) {
99127
$storeId = $this->getDefaultStoreId();
100128
$connection->delete(
101129
$table,
@@ -107,17 +135,24 @@ protected function _saveAttributeValue($object, $attribute, $value)
107135
);
108136
}
109137

110-
$data = new \Magento\Framework\DataObject(
111-
[
112-
'attribute_id' => $attribute->getAttributeId(),
113-
'store_id' => $storeId,
114-
$this->getLinkField() => $entityId,
115-
'value' => $this->_prepareValueForSave($value, $attribute),
116-
]
117-
);
138+
$data = $attribute->isStatic()
139+
? new \Magento\Framework\DataObject(
140+
[
141+
$this->getLinkField() => $entityId,
142+
$attribute->getAttributeCode() => $this->_prepareValueForSave($value, $attribute),
143+
]
144+
)
145+
: new \Magento\Framework\DataObject(
146+
[
147+
'attribute_id' => $attribute->getAttributeId(),
148+
'store_id' => $storeId,
149+
$this->getLinkField() => $entityId,
150+
'value' => $this->_prepareValueForSave($value, $attribute),
151+
]
152+
);
118153
$bind = $this->_prepareDataForTable($data, $table);
119154

120-
if ($attribute->isScopeStore()) {
155+
if ($attribute->isScopeStore() || $attribute->isStatic()) {
121156
/**
122157
* Update attribute value for store
123158
*/
@@ -143,6 +178,8 @@ protected function _saveAttributeValue($object, $attribute, $value)
143178
}
144179

145180
/**
181+
* Resolve entity id
182+
*
146183
* @param int $entityId
147184
* @return int
148185
*/

app/code/Magento/Eav/Model/Entity/AbstractEntity.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,8 @@ public function load($object, $entityId, $attributes = [])
10211021
protected function loadAttributesMetadata($attributes)
10221022
{
10231023
$this->loadAttributesForObject($attributes);
1024+
1025+
return $this;
10241026
}
10251027

10261028
/**
@@ -1433,8 +1435,10 @@ protected function _processSaveData($saveData)
14331435
$insertEntity = true;
14341436
$entityTable = $this->getEntityTable();
14351437
$entityIdField = $this->getEntityIdField();
1438+
// phpstan:ignore "Undefined variable"
14361439
$entityId = $newObject->getId();
14371440

1441+
// phpstan:ignore "Undefined variable"
14381442
unset($entityRow[$entityIdField]);
14391443
if (!empty($entityId) && is_numeric($entityId)) {
14401444
$bind = ['entity_id' => $entityId];
@@ -1450,6 +1454,7 @@ protected function _processSaveData($saveData)
14501454
/**
14511455
* Process base row
14521456
*/
1457+
// phpstan:ignore "Undefined variable"
14531458
$entityObject = new DataObject($entityRow);
14541459
$entityRow = $this->_prepareDataForTable($entityObject, $entityTable);
14551460
if ($insertEntity) {
@@ -1460,6 +1465,7 @@ protected function _processSaveData($saveData)
14601465
$connection->insert($entityTable, $entityRow);
14611466
$entityId = $connection->lastInsertId($entityTable);
14621467
}
1468+
// phpstan:ignore "Undefined variable"
14631469
$newObject->setId($entityId);
14641470
} else {
14651471
$where = sprintf('%s=%d', $connection->quoteIdentifier($entityIdField), $entityId);
@@ -1472,6 +1478,7 @@ protected function _processSaveData($saveData)
14721478
if (!empty($insert)) {
14731479
foreach ($insert as $attributeId => $value) {
14741480
$attribute = $this->getAttribute($attributeId);
1481+
// phpstan:ignore "Undefined variable"
14751482
$this->_insertAttribute($newObject, $attribute, $value);
14761483
}
14771484
}
@@ -1482,6 +1489,7 @@ protected function _processSaveData($saveData)
14821489
if (!empty($update)) {
14831490
foreach ($update as $attributeId => $v) {
14841491
$attribute = $this->getAttribute($attributeId);
1492+
// phpstan:ignore "Undefined variable"
14851493
$this->_updateAttribute($newObject, $attribute, $v['value_id'], $v['value']);
14861494
}
14871495
}
@@ -1491,12 +1499,14 @@ protected function _processSaveData($saveData)
14911499
*/
14921500
if (!empty($delete)) {
14931501
foreach ($delete as $table => $values) {
1502+
// phpstan:ignore "Undefined variable"
14941503
$this->_deleteAttributes($newObject, $table, $values);
14951504
}
14961505
}
14971506

14981507
$this->_processAttributeValues();
14991508

1509+
// phpstan:ignore "Undefined variable"
15001510
$newObject->isObjectNew(false);
15011511

15021512
return $this;
@@ -1573,7 +1583,7 @@ protected function _processAttributeValues()
15731583
{
15741584
$connection = $this->getConnection();
15751585
foreach ($this->_attributeValuesToSave as $table => $data) {
1576-
$connection->insertOnDuplicate($table, $data, ['value']);
1586+
$connection->insertOnDuplicate($table, $data, array_keys($data[0]));
15771587
}
15781588

15791589
foreach ($this->_attributeValuesToDelete as $table => $valueIds) {
@@ -1607,7 +1617,9 @@ protected function _prepareValueForSave($value, AbstractAttribute $attribute)
16071617
self::$_attributeBackendTables[$backendTable] = $this->getConnection()->describeTable($backendTable);
16081618
}
16091619
$describe = self::$_attributeBackendTables[$backendTable];
1610-
return $this->getConnection()->prepareColumnValue($describe['value'], $value);
1620+
$columnName = $attribute->isStatic() ? $attribute->getAttributeCode() : 'value';
1621+
1622+
return $this->getConnection()->prepareColumnValue($describe[$columnName], $value);
16111623
}
16121624

16131625
/**

0 commit comments

Comments
 (0)