Skip to content

Fixed Magento2 Attribute option does not validate for existing records before insert #20852

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

Conversation

ravi-chandra3197
Copy link
Contributor

@ravi-chandra3197 ravi-chandra3197 commented Jan 31, 2019

Description (*)

Fixed Magento2 Attribute option does not validate for existing records before insert

Fixed Issues (if relevant)

  1. Magento2 Attribute option does not validate for existing records before insert #16852: Magento2 Attribute option does not validate for existing records before insert

Manual testing scenarios (*)

  1. Create a product eav attribute dynamically using class (Magento\Catalog\Model\ResourceModel\Eav\Attribute) and set the entity type Id to the type id of catalog_product (See sample code below) (eg: attribute code = colors)
  2. generate a list of possible options and then add those options into that product EAV attribute by using (Magento\Eav\Setup\EavSetup -> AddOption()) (optionarray = 'Black, Blue, White')
  3. Generate some more options (pick some of those which are already existed) and add then add them into the product attribute by following the same step describe in step 2. ('White, Green, Purple')

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds on Travis CI are green)

@magento-engcom-team
Copy link
Contributor

Hi @ravi-chandra3197. Thank you for your contribution
Here is some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento-engcom-team give me test instance - deploy test instance based on PR changes
  • @magento-engcom-team give me 2.3-develop instance - deploy vanilla Magento instance

For more details, please, review the Magento Contributor Assistant documentation

Copy link
Contributor

@swnsma swnsma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking option existence with attribute_id and sort_order is the not best option.
From my point of view, query to check should be like this:
select * from eav_attribute_option as eao inner join eav_attribute_option_value as eaov using (option_id) where value = 'Black' and store_id = 0 and attribute_id = 93;
So we do next:

  1. Check if option exists.
  2. If option does not exist - create new option.
  3. If option exists - check the sort order of the option.
  4. If option exists in DB and option in the request has other sort order than option in DB - update the sort order.
  5. If option in DB exists and has the same sort order in DB - do nothing.

@ravi-chandra3197
Copy link
Contributor Author

Hello @swnsma
I have update my code can you review

@swnsma
Copy link
Contributor

swnsma commented Feb 4, 2019

Hi @ravi-chandra3197
Looks like this is not the case, because variable $intOptionId is not initialized.
Please check my previous comment.

@swnsma swnsma self-assigned this Feb 4, 2019
@ravi-chandra3197
Copy link
Contributor Author

Hello @swnsma
I have update my code can you review

@swnsma
Copy link
Contributor

swnsma commented Feb 4, 2019

Hi @ravi-chandra3197
Thank you for your changes.
Look like your last commit has problems with the PHP syntax.
At least, I have received next message when tried to run it:

PHP Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ',' or ')'

Could you please check?

@ravi-chandra3197
Copy link
Contributor Author

Hello @swnsma
I have update my code can you review

@swnsma
Copy link
Contributor

swnsma commented Feb 5, 2019

Hi @ravi-chandra3197,

Look like usage of \Magento\Framework\DB\Adapter\AdapterInterface::select is not meet by method declaration in \Magento\Framework\DB\Adapter\AdapterInterface:

  1. Method does not expect parameters.
  2. Method always return object of class \Magento\Framework\DB\Select.

@swnsma
Copy link
Contributor

swnsma commented Feb 17, 2019

Hi @ravi-chandra3197,
I am closing this PR now due to inactivity.
Please reopen and update if you wish to continue.
Thank you for the collaboration!

@swnsma swnsma closed this Feb 17, 2019
@ghost
Copy link

ghost commented Feb 17, 2019

Hi @ravi-chandra3197, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

@ravi-chandra3197
Copy link
Contributor Author

Hello @swnsma
I have update my code can you review

Copy link
Member

@sivaschenko sivaschenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ravi-chandra3197 thanks for the update. Can you please fix code styles and take a look at my code review note.

@@ -930,13 +930,16 @@ public function addAttributeOption($option)
}
} elseif (isset($option['values'])) {
foreach ($option['values'] as $sortOrder => $label) {
$checkOpionId = $this->setup->getConnection()->select($optionTable, ['attribute_id =?' =>$option['attribute_id'], 'sort_order =?' => $sortOrder, 'value =?'=> $label]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider moving this select out of the loop if possible

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have to check each option before insert that's I have put select inside loop.
Any suggestion?

Copy link
Contributor

@orlangur orlangur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ravi-chandra3197 this is not a right way to validate uniqueness.

Unique index must be added on database level instead and then constraint violation properly handled on PHP side.

@ghost ghost assigned orlangur Apr 6, 2019
@ravi-chandra3197
Copy link
Contributor Author

ravi-chandra3197 commented Apr 17, 2019

@ravi-chandra3197 this is not a right way to validate uniqueness.

Unique index must be added on database level instead and then constraint violation properly handled on PHP side.

Hello @orlangur
I have checked in DB and found that it's not possible to add Unique index because of its EAV table for store value, it's a possibility to repeat the same value for other attributes option.
like the color attribute black option value repeat in other custom attributes bg_color attribute also have a black option value,
In the above situation, it's not possible to add Unique index for the eav_attribute_option_value table.

@orlangur
Copy link
Contributor

@ravi-chandra3197 not the option value must be unique but a combination <attribute, option_label> or even <attribute, option_label, store_id>.

@ravi-chandra3197
Copy link
Contributor Author

ravi-chandra3197 commented Apr 17, 2019

@ravi-chandra3197 not the option value must be unique but a combination <attribute, option_label> or even <attribute, option_label, store_id>.

@orlangur
In eav_attribute_option table only store
option_id, attribute_id, sort_order

https://prnt.sc/nd3tng

eav_attribute_option_value table only store
value_id, option_id, store_id, value

https://prnt.sc/nd3twl

so it's not possible to resolve issue using adding Unique index for any of above table.

@orlangur
Copy link
Contributor

Thanks for explanation, @ravi-chandra3197, finally figured it out. Closing PR in favor of abovementioned one.

@orlangur orlangur closed this Apr 17, 2019
@m2-assistant
Copy link

m2-assistant bot commented Apr 17, 2019

Hi @ravi-chandra3197, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

@ravi-chandra3197
Copy link
Contributor Author

@ravi-chandra3197 not the option value must be unique but a combination <attribute, option_label> or even <attribute, option_label, store_id>.

@orlangur
In eav_attribute_option table only store
option_id, attribute_id, sort_order

https://prnt.sc/nd3tng

eav_attribute_option_value table only store
value_id, option_id, store_id, value

https://prnt.sc/nd3twl

so it's not possible to resolve issue using adding Unique index for any of above table.

@orlangur
In backend it's validate attribute option value using unique array value on ajax call.
for validate attribute option using setup, it's only one way which I have use in this PR

@orlangur
Copy link
Contributor

@ravi-chandra3197 why did you reopen this PR?

@ravi-chandra3197
Copy link
Contributor Author

@ravi-chandra3197 why did you reopen this PR?

@orlangur
Because my PR has a proper solution.
it's not possible to add Unique index in DB.

in the backend, it uses ajax call validates unique attribute option value in an array.
for adding attribute option value using setup script there is only one way validate which I have proposed in this PR.

@orlangur
Copy link
Contributor

@ravi-chandra3197 could your please read my comment again?

@orlangur
Copy link
Contributor

Hi @ravi-chandra3197, so basically #21424 solves the same issue.

@orlangur orlangur closed this Jun 22, 2019
@m2-assistant
Copy link

m2-assistant bot commented Jun 22, 2019

Hi @ravi-chandra3197, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants