magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.46k stars 9.28k forks source link

Cannot save only group_id using \Magento\Customer\Model\ResourceModel\Customer::saveAttribute #35699

Open ioweb-gr opened 2 years ago

ioweb-gr commented 2 years ago

Preconditions and environment

Steps to reproduce

Use the following sample code

``{{php

public function __construct( CustomerRepositoryInterface $customerRepository, Customer $customerResourceModel, CustomerFactory $customerFactory }

{ $this->customerRepository = $customerRepository; $this->customerResourceModel = $customerResourceModel; $this->customerFactory = $customerFactory; }

}}``

And then


$customer = $this->customerFactory()->create();
$this->customerResourceModel->load($customer, <id>);
$customer->setGroupId(<new_id>);
$this->customerResourceModel->saveAttribute($customer, 'group_id');

Expected result

The customer group_id is changed

Actual result

SQLSTATE<42S22>: Column not found: 1054
Unknown column 'customer*entity.value_id' in 'field list', query was: SELECT `customer_entity\`.`value*id`,
`customer*entity\`.`value\` FROM `customer_entity` WHERE (attribute_id='10' AND entity*id='7218')

Additional information

It seems that the \Magento\Eav\Model\Entity\AbstractEntity::saveAttribute function cannot properly save the value because it's building the wrong select query.

SELECT `customer*entity\`.`value_id\`, `customer_entity\`.`value\` FROM `customer_entity` WHERE (attribute_id='10' AND entity*id='7352')

Instead of using

SELECT `customer*entity\`.`group_id\`, `customer_entity\`.`value\` FROM `customer_entity` WHERE (entity*id='7352')

So it has an issue with static attributes.

Similar Issue

There is a similar issue here #35064. Copying its content here and marking that issue as a duplicate of this one

unable to save Customer's billing address with {{{}saveAttribute{}}}:

  1. Create customer (ex. ID = 100500).
  2. Create address for customer (ex. ID = 123).
  3. Run code

 `` $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

/* @var \Magento\Customer\Model\Customer $customer */ $customer = $objectManager->create(\Magento\Customer\Model\Customer::class); / @var \Magento\Customer\Model\ResourceModel\Customer $customerResource **/ $customerResource = $objectManager->get(\Magento\Customer\Model\ResourceModel\Customer::class); $customerResource->load($customer, 100500);

$customer->setDefaultBilling(123); $customerResource->saveAttribute($customer, 'default_billing'); `#

  1.  

Release note

No response

Triage and priority

m2-assistant[bot] commented 2 years ago

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

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.


:clock10: You can find the schedule on the Magento Community Calendar page.

:telephone_receiver: 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.

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

m2-assistant[bot] commented 2 years ago

Hi @engcom-Hotel. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

engcom-Hotel commented 2 years ago

Hello @ioweb-gr,

Thanks for the report and collaboration!

We have tried to reproduce the issue in Magento 2.4-develop branch. Actually, I have a suggestion here, no need to save group_id as an attribute. The customer_entity table has group_id field. So we can set it through the customer model as given below:

        $customer = $this->customerFactory->create();
        $this->customerResourceModel->load($customer, 1);
        $customer->setGroupId(3);
        $customer->save();

Let me know in case of a query.

Thanks

ioweb-gr commented 2 years ago

@engcom-Hotel Saving the whole customer is a actually a much heavier process as it saves both the customer model and his address.

We have over 50k customers which need to be processed daily for group changes based on their order totals, so saving the whole customer including his address every single time is consuming a lot of resources especially memory-wise. Moreover the time of processing is much bigger like this.

That's why I'd prefer to use the saveAttribute function which should function correctly for group_id as well.

However it doesn't work.

engcom-Hotel commented 2 years ago

Hello @ioweb-gr,

Thanks for the reply!

As we have a workaround for this issue and seems that the workaround is very heavy in terms of memory consumption I think we can mark this issue as a feature request.

Please suggest.

Thanks

ioweb-gr commented 2 years ago

How is it considered a workaround?

Saving two models instead of a single attribute is not a workaround in my opinion.

Even if you consider it so, the issue is still that saveAttribute function is not working as expected. It should be able to save the group id.

This is not a feature request, it's broken functionality.

saveAttribute is supposed to be able to save a single attribute correctly

engcom-Hotel commented 2 years ago

Hello @ioweb-gr,

Thanks for your reply!

As per the discussion, we are not able to use the saveAttribute method in order to save group_id. Hence we are confirming this issue for further processing.

Thanks

github-jira-sync-bot commented 2 years ago

:white_check_mark: Jira issue https://jira.corp.magento.com/browse/AC-5894 is successfully created for this GitHub issue.

m2-assistant[bot] commented 2 years ago

:white_check_mark: Confirmed by @engcom-Hotel. Thank you for verifying the issue.
Issue Available: @engcom-Hotel, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

ioweb-gr commented 1 year ago

@engcom-Hotel Let me add here that it's not possible to update static attributes for products either. I created a custom attribute which is static and is added as a table column in catalog_product_entity. I can update it just fine using $productRepository->save or with $model->save or $resourceModel->save($product)

However $resourceModel->saveAttribute() won't work throwing an exception.

For example in my case

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'catalog_product_entity.value_id' in 'field list', query was: SELECT `catalog_product_entity`.`value_id`, `catalog_product_entity`.`value` FROM `catalog_product_entity` WHERE (attribute_id='838' AND entity_id='1209' AND store_id=1)

The same core issue is for every static attribute in all entities it seems and it's a general problem. When doing mass updates of products, you don't need to always save the whole product which is a very taxing operation just to update a single attribute.

Valid use cases where this might be needed.

ERP synchronization API synchronization Marketplaces synchronization Newsletter subscriber synchronization

All of them need to store a mapping id as a static field usually and saveAttribute should be usable in these cases and an eav attribute is not needed in such cases. A static field will perform better.