Magento2: Problem adding attribute options that start with a number via REST Api

Created on 21 Jul 2016  路  14Comments  路  Source: magento/magento2

Steps to reproduce

  1. Via REST API make a POST request to: /V1/products/attributes/{attributeCode}/options
  2. send up new data with label and value that begins with a number e.g. "123 test"

    Expected result

  3. new attribute option should be added the same as when label and value begin with a letter.

  4. API result is "StatusCode: OK, Content-Type: application/json; charset=utf-8, Content-Length: 4)"

    Actual result

  5. response from API call is: {"message":"Cannot save attribute %1","parameters":["size"]}

This only happens when the attribute options starts with a number.

Format is not valid Ready for Work bug report

Most helpful comment

I have created a external file in magneto 2 and it worked

<?php
use Magento\Framework\App\Bootstrap;

require __DIR__ . '/app/bootstrap.php';

$params = $_SERVER;

$bootstrap = Bootstrap::create(BP, $params);

try{
$obj = $bootstrap->getObjectManager();

$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$eavSetupFactory = $obj->create('\Magento\Eav\Setup\EavSetup');
$attributeFactory = $obj->get('\Magento\Catalog\Model\ResourceModel\Eav\Attribute'); 

$attribute_arr = ['35 mm','45 mm','65 mm','aman', 'gurdit' ,'28','39'];


$attributeInfo = $attributeFactory->getCollection()
               ->addFieldToFilter('attribute_code',['eq'=>"size"])
               ->getFirstItem();
 $attribute_id = $attributeInfo->getAttributeId();

 $option=array();
$option['attribute_id'] = $attribute_id;
foreach($attribute_arr as $key=>$value){

    $str = '"'.$value.'"';

    $option['value'][$str][0]= str_replace('"','', $str);
}

$eavSetupFactory->addAttributeOption($option);
}catch(Exception $e){

    echo $e->getMessage();
}
?>

All 14 comments

+1: I can confirm.
Below the 'Cannot save attribute %s' I get the Exception

[Zend_Db_Statement_Exception]) SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (db_name.eav_attribute_option_value, CONSTRAINTEAV_ATTR_OPT_VAL_OPT_ID_EAV_ATTR_OPT_OPT_IDFOREIGN KEY (option_id) REFERENCESeav_attribute_option(option_id) ON DELETE ), query was: INSERT INTOeav_attribute_option_value(option_id,store_id,value) VALUES (?, ?, ?)))

from the Magento\Eav\Model\ResourceModel\Entity\Attribute class.

This problem is not restricted to API calls!

I was using the function add from the Magento\Eav\Model\Entity\Attribute\OptionManagement class.
Meanwhile no problems using value-strings starting with alpha characters.

@johnhird @jBOKA thank you for reporting!
We reproduced the issue and created internal MAGETWO-58338.

Fix of MAGETWO-58338 is delivered to develop branch. Closing the issue.

Hi, as far as can see the issue still exists in 2.1.2
How do we know, when the fix is delivered to the composer packages?

In 2.2.0 release, IMHO.

Was there a fix here? I see an update to a unit test in MAGETWO-58338, but not to the code that would fix the problem.

@worldpac fix was delivered to develop branch in commits 4f6281da and 21d58ef7.

2.1.6 still experiencing

This issue is persisting in 2.1.7, i was hoping it must be resolved.

I have the same error importing attributes with Firegento FastSimpleImport module. I've tried to apply 21d58ef but it doens't work. Is there any workaround?

Problem still exist: https://github.com/magento/magento2/blob/2.1.10-preview/app/code/Magento/Eav/Model/ResourceModel/Entity/Attribute.php

You don't add this fix [https://github.com/magento/magento2/commit/21d58ef7b7af29ebfea183dbe5e4a084a5b2c343] to 2.1.9!

I have created a external file in magneto 2 and it worked

<?php
use Magento\Framework\App\Bootstrap;

require __DIR__ . '/app/bootstrap.php';

$params = $_SERVER;

$bootstrap = Bootstrap::create(BP, $params);

try{
$obj = $bootstrap->getObjectManager();

$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$eavSetupFactory = $obj->create('\Magento\Eav\Setup\EavSetup');
$attributeFactory = $obj->get('\Magento\Catalog\Model\ResourceModel\Eav\Attribute'); 

$attribute_arr = ['35 mm','45 mm','65 mm','aman', 'gurdit' ,'28','39'];


$attributeInfo = $attributeFactory->getCollection()
               ->addFieldToFilter('attribute_code',['eq'=>"size"])
               ->getFirstItem();
 $attribute_id = $attributeInfo->getAttributeId();

 $option=array();
$option['attribute_id'] = $attribute_id;
foreach($attribute_arr as $key=>$value){

    $str = '"'.$value.'"';

    $option['value'][$str][0]= str_replace('"','', $str);
}

$eavSetupFactory->addAttributeOption($option);
}catch(Exception $e){

    echo $e->getMessage();
}
?>

Still an issue in 2.2.7

Still an issue in 2.3.3 and 2.3.4 - please fix. What's the point in InstallData files if we have to create certain values through the admin panel?

Was this page helpful?
0 / 5 - 0 ratings