Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot save sku using ResourceModel saveAttribute function #36084

Closed
1 of 5 tasks
ioweb-gr opened this issue Sep 4, 2022 · 5 comments
Closed
1 of 5 tasks

Cannot save sku using ResourceModel saveAttribute function #36084

ioweb-gr opened this issue Sep 4, 2022 · 5 comments
Labels
Issue: ready for confirmation Reported on 2.4.3-p1 Indicates original Magento version for the Issue report. Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it

Comments

@ioweb-gr
Copy link
Contributor

ioweb-gr commented Sep 4, 2022

Preconditions and environment

  • 2.4.3-p1

Steps to reproduce

Create a product with SKU test1
Run the following standalone script located in pub/tools/ folder

<?php
/*
 * Copyright (c) 2022. IOWEB TECHNOLOGIES
 */

ini_set('display_errors', 1);

use Magento\Framework\App\Bootstrap;
require __DIR__ . '/../../app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);

$om = $bootstrap->getObjectManager();
$state = $om->get(Magento\Framework\App\State::class);
$state->setAreaCode('adminhtml');

$productResource = $om->get(\Magento\Catalog\Model\ResourceModel\Product::class);
$productRepository = $om->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
$product = $productRepository->get('test1');
$product->setSku('test');
$productResource->saveAttribute($product, 'sku');

You will get an exception

Fatal error: Uncaught PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'catalog_product_entity.value_id' in 'field list' in /var/www/vhosts/website/httpdocs/vendor/magento/framework/DB/Statement/Pdo/Mysql.php:91 Stack trace: #0 /var/www/vhosts/website/httpdocs/vendor/magento/framework/DB/Statement/Pdo/Mysql.php(91): PDOStatement->execute(Array) #1 /var/www/vhosts/website/httpdocs/vendor/magento/framework/DB/Statement/Pdo/Mysql.php(107): Magento\Framework\DB\Statement\Pdo\Mysql->Magento\Framework\DB\Statement\Pdo\{closure}() #2 /var/www/vhosts/website/httpdocs/vendor/magento/framework/DB/Statement/Pdo/Mysql.php(92): Magento\Framework\DB\Statement\Pdo\Mysql->tryExecute(Object(Closure)) #3 /var/www/vhosts/website/httpdocs/vendor/magento/zendframework1/library/Zend/Db/Statement.php(303): Magento\Framework\DB\Statement\Pdo\Mysql->_execute(Array) #4 /var/www/vhosts/website/httpdocs/vendor/magento/zendframework1/li in /var/www/vhosts/website/httpdocs/vendor/magento/framework/DB/Statement/Pdo/Mysql.php on line 110

Expected result

The sku is saved properly

Actual result

An exception is thrown


Fatal error: Uncaught PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'catalog_product_entity.value_id' in 'field list' in /var/www/vhosts/website/httpdocs/vendor/magento/framework/DB/Statement/Pdo/Mysql.php:91 Stack trace: #0 /var/www/vhosts/website/httpdocs/vendor/magento/framework/DB/Statement/Pdo/Mysql.php(91): PDOStatement->execute(Array) #1 /var/www/vhosts/website/httpdocs/vendor/magento/framework/DB/Statement/Pdo/Mysql.php(107): Magento\Framework\DB\Statement\Pdo\Mysql->Magento\Framework\DB\Statement\Pdo\{closure}() #2 /var/www/vhosts/website/httpdocs/vendor/magento/framework/DB/Statement/Pdo/Mysql.php(92): Magento\Framework\DB\Statement\Pdo\Mysql->tryExecute(Object(Closure)) #3 /var/www/vhosts/website/httpdocs/vendor/magento/zendframework1/library/Zend/Db/Statement.php(303): Magento\Framework\DB\Statement\Pdo\Mysql->_execute(Array) #4 /var/www/vhosts/website/httpdocs/vendor/magento/zendframework1/li in /var/www/vhosts/website/httpdocs/vendor/magento/framework/DB/Statement/Pdo/Mysql.php on line 110

Additional information

I'm in the process of creating an automation for generating product SKUs based on a series and I was tyring to create a cron job that will automatically alter the SKUs to the appropriate format.
However this issue occured and I'm now unable to do it in a fast way and I have to use the ProductRepository to save the whole product instead of just modifying the attribute itself.

That means one extra load for the product and one extra save for the whole product instead of just saving my requested attribute

In my opinion saveAttribute should be able to save the sku as well but it seems it only works for non-static attributes.

Release note

No response

Triage and priority

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.
@m2-assistant
Copy link

m2-assistant bot commented Sep 4, 2022

Hi @ioweb-gr. Thank you for your report.
To speed up processing of this issue, make sure that you provided the following information:

  • Summary of the issue
  • Information on your environment
  • Steps to reproduce
  • Expected and actual results

Make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, review the Magento Contributor Assistant documentation.

Add a comment to assign the issue: @magento I am working on this

To learn more about issue processing workflow, refer to the Code Contributions.


⚠️ According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.

🕙 You can find the schedule on the Magento Community Calendar page.

📞 The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

✏️ Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

@rogerdz
Copy link
Contributor

rogerdz commented Sep 4, 2022

same #7247

@engcom-Hotel engcom-Hotel added Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it Reported on 2.4.3-p1 Indicates original Magento version for the Issue report. labels Sep 5, 2022
@ioweb-gr
Copy link
Contributor Author

ioweb-gr commented Oct 3, 2022

But that one refers to addAttributeUpdate and not save attribute so it's different area

@andrewbess
Copy link

andrewbess commented Oct 19, 2022

Hello @ioweb-gr
The sku is static attribute. This is field in catalog_product_entity table and is different from attribute in EAV conception.
For example from issue please use
$productRepository->save($product);
or
$productResource->save($product);
But I recommend first way.

I am sure this is not bug and issue can be closed.

@ioweb-gr
Copy link
Contributor Author

So what is the recommended way to update only one field or static attribute without saving the whole model?

Shouldn't there be an equivalent for those fields as well like saveField or something?

If not, shouldn't there be a documented way to do this properly and efficiently as it apparently affects all static attributes or fields which are saved in any entity on a table column and not in eav structures.

For products specifically, I think that saving a model with 60+ attributes to update and firing all observers and plugins when you need to update only one column is a waste of resources and leaves us with no workaround except for running direct sql queries.

It may not be a bug in the way you mentioned it but it is a limitation or lack of documentation, whatever you may call it. In any case this area needs improvement and the issue is reasonable.

You could have marked it as dev experience instead of closing in my humble opinion as all closed issues are never processed even if they have validity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue: ready for confirmation Reported on 2.4.3-p1 Indicates original Magento version for the Issue report. Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it
Projects
None yet
Development

No branches or pull requests

4 participants