magento2: Products updated_at field is not updating on save

Preconditions

  1. Magento 2.1.1, 2.1.0 - fresh install (no sample data)
  2. Not checked in previous versions 2.0.x
  3. PHP 5.6.25
  4. MySQL 5.6.28
  5. Product types: all

Steps to reproduce

  1. Go to admin panel
  2. Create new product (fill all required data, category, related, upsell etc is not improtant)
  3. In catalog_product_entity table check updated_at value (it is same like created_at)
  4. Than go to edit same product
  5. Update any product’s value like: category, description, short_description. Do not update SKU (basically do not update any field from catalog_product_entity table)
  6. Save the product
  7. Check updated_at value -> value is not changed

Expected result

  1. After updating any products data updated_at column in catalog_product_entity should be updated. Not just when SKU is changed.

Actual result

  1. updated_at column in catalog_product_entity table keep same value from moment when product is created.

Problem Analyzes

Analyzing catalog_product_entity table it is clear that Default value for that field is CURRENT_TIMESTAMP and Extra is “on update CURRENT_TIMESTAMP”.

I have debugging the issue and found source of the problem. When product is saved and if product is not new this class is responsible for updating data in entity table \Magento\Framework\EntityManager\Db\UpdateRow (vendor/magento/framework/EntityManager/Db/UpdateRow.php)

Method “execute” is receiving all product’s data in $data variable including new correct value for updated_at field. On line 81 method prepareData return only fields which can be stored in catalog_product_entity table. But in the list of values updated_at is missing.

Now if we look inside prepareData method in the same class, on line 53 all columns which have CURRENT_TIMESTAMP for default values are ignored. And that is the source of problem.

Even if updated_at column has “on update CURRENT_TIMESTAMP” definition it will not update value automatically if values of columns in catalog_product_entity are not changed.

For example there is a product with SKU “testsku” in this case:

UPDATE catalog_product_entity SET sku = 'testsku' WHERE entity_id = 1;

updated_at field will not be updated and will not get new value. However, when product is saved it can get new Name or Description but updated_at info will not be changed.

However if query set new value for SKU field MySQL automatically will set CURRENT_TIMESTAMP to updated_at field. So this query will work:

UPDATE catalog_product_entity SET sku = 'newsku' WHERE entity_id = 1;

Conclusion is that fields which has DEFAULT value CURRENT_TIMESTAMP and “on update CURRENT_TIMESTAMP” definition should not be removed from $data array before save because values will not be updated. In this case it is clear that updated_at value will be changed only if value of any column is changed only in catalog_product_entity table but product entity contain multiple attribute values and updated_at need to be changed when any attribute value for product is changed on save.

Updated at value is important for example in case that we want to check if product is updated and for example pull new data to third party application or something.

Please let me know if you have any questions.

Thanks Milos

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 24 (5 by maintainers)

Most upvoted comments

@veloraven I think #5385 is a different issue.

Repeating the original steps to reproduce on latest develop still gives us the “Actual Result” described here: https://github.com/magento/magento2/issues/6683#issue-178195771

Can you please re-open this issue?

This is causing us to lose a lot of admin edits when integrating the Magento catalog with third party systems (using updated_at to retrieve product changes and merge this with those from other sources).

Since my project depends on the updated_at field I created a simple module to solve this without hacking core. I know it’s not a very neet fix, but it does it job. I’ve tried saving product using model in custom script and also in admin. And it works for me on Magento 2.1.4.

Created a module if anyone else was in need of the same functionality:

https://github.com/codepeak/magento2-productfix