Skip to content

Custom Product Attribute changes 'backend_type' when 'is_user_defined = 1' and get updated/saved in Admin Backend #9219

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

Closed
mhauri opened this issue Apr 12, 2017 · 6 comments
Labels
Fixed in 2.2.x The issue has been fixed in 2.2 release line Fixed in 2.3.x The issue has been fixed in 2.3 release line Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release

Comments

@mhauri
Copy link

mhauri commented Apr 12, 2017

Preconditions

  1. Magento: 2.1.4 to 2.1.6, maybe earlier versions are affected too.

Steps to reproduce

  1. Create a custom Product attribute via Setup of a Custom Module:
[
    'required' => 0,
    'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
    'group' => 'Test',
    'searchable' => 0,
    'comparable' => 0,
    'used_for_sort_by' => 0,
    'used_in_product_listing' => 0,
    'visible_in_advanced_search' => 0,
    'user_defined' => 1,
    'is_html_allowed_on_front' => 1,
    'visible_on_front' => 1,
    'input' => 'select',
    'source' => 'Test\Test\Model\Attribute\Source\Discount',
    'backend_type' => 'varchar',
    'label' => 'Discount',
    'position' => 1,
]
  1. Go to the Backend Stores > Attributes > Product
  2. Click on the newly created attribute and save it (with or without modifications).

Expected result

  1. The backend_type doesn't change for the custom attribute.

Actual result

  1. The backend_type got changed from varchar to int
@mhauri
Copy link
Author

mhauri commented Apr 12, 2017

After digging into the code the problem starts in the file: module-catalog/Controller/Adminhtml/Product/Attribute/Save.php on line 199

if (is_null($model->getIsUserDefined()) || $model->getIsUserDefined() != 0) {
    $data['backend_type'] = $model->getBackendTypeByInput($data['frontend_input']);
}

So when the Attribute is user defined $model->getIsUserDefined() != 0 then the backend_type will be set based on the frontend_input $model->getBackendTypeByInput($data['frontend_input'])

The method getBackendTypeByInput() is defined in: module-eav/Model/Entity/Attribute.php

/**
     * Detect backend storage type using frontend input type
     *
     * @param string $type frontend_input field value
     * @return string backend_type field value
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     */
    public function getBackendTypeByInput($type)
    {
        $field = null;
        switch ($type) {
            case 'text':
            case 'gallery':
            case 'media_image':
            case 'multiselect':
                $field = 'varchar';
                break;

            case 'image':
            case 'textarea':
                $field = 'text';
                break;

            case 'date':
                $field = 'datetime';
                break;

            case 'select':
            case 'boolean':
                $field = 'int';
                break;

            case 'price':
            case 'weight':
                $field = 'decimal';
                break;

            default:
                break;
        }

        return $field;
    }

By this method the frontend_input: select will always return int
As far as I can see there are core attributes which also have select as frontend_input and varchar as backend_type.

For the moment the solution will be to change the custom attribute to is_user_defined = 0 but I think this should be handled different for user defined attributes by not setting the backend_type based on the frontend_input

Or are there any reasons this is handled this way?

@magento-engcom-team magento-engcom-team added G1 Passed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed and removed G1 Passed labels Sep 5, 2017
@magento-engcom-team magento-engcom-team added the Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed label Oct 4, 2017
@magento-engcom-team magento-engcom-team self-assigned this Oct 4, 2017
@magento-engcom-team magento-engcom-team added 2.1.x Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release labels Oct 4, 2017
@magento-engcom-team
Copy link
Contributor

@mhauri, thank you for your report.
We've created internal ticket(s) MAGETWO-80862 to track progress on the issue.

@keyurshah070
Copy link
Contributor

keyurshah070 commented Oct 22, 2017

should I commit ? After checking long conversion I am confuse(whether it is merged or not) in status @okorshenko @ishakhsuvarov

@keyurshah070
Copy link
Contributor

I'm working on it #SQUASHTOBERFEST @okorshenko @ishakhsuvarov

@sidolov
Copy link
Contributor

sidolov commented Oct 12, 2018

Hi @mhauri. Thank you for your report.
The issue has been fixed in #18196 by @bartoszkubicki in 2.2-develop branch
Related commit(s):

The fix will be available with the upcoming 2.2.8 release.

@slavvka
Copy link
Member

slavvka commented Oct 22, 2018

Hi @mhauri. Thank you for your report.
The issue has been fixed in #18570 by @gelanivishal in 2.3-develop branch
Related commit(s):

The fix will be available with the upcoming 2.3.1 release.

@slavvka slavvka added the Fixed in 2.3.x The issue has been fixed in 2.3 release line label Oct 22, 2018
magento-devops-reposync-svc pushed a commit that referenced this issue Sep 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fixed in 2.2.x The issue has been fixed in 2.2 release line Fixed in 2.3.x The issue has been fixed in 2.3 release line Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release
Projects
None yet
Development

No branches or pull requests

6 participants