-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Fixed Magento2 Attribute option does not validate for existing records before insert #20852
Conversation
Hi @ravi-chandra3197. Thank you for your contribution
For more details, please, review the Magento Contributor Assistant documentation |
There was a problem hiding this 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:
- Check if option exists.
- If option does not exist - create new option.
- If option exists - check the sort order of the option.
- If option exists in DB and option in the request has other sort order than option in DB - update the sort order.
- If option in DB exists and has the same sort order in DB - do nothing.
…s before insert (check option id)
Hello @swnsma |
Hi @ravi-chandra3197 |
…s before insert (check value of option)
Hello @swnsma |
Hi @ravi-chandra3197
Could you please check? |
…s before insert (update change)
…s before insert (check label)
Hello @swnsma |
Look like usage of \Magento\Framework\DB\Adapter\AdapterInterface::select is not meet by method declaration in \Magento\Framework\DB\Adapter\AdapterInterface:
|
Hi @ravi-chandra3197, |
Hi @ravi-chandra3197, thank you for your contribution! |
…s before insert (check size of select)
Hello @swnsma |
There was a problem hiding this 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]); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
…s before insert (check with fetchrow)
…s before insert (fix travis)
…s before insert (check fetchRow)
…s before insert (check fetchAll)
…s before insert (check row data)
…s before insert (check row)
…s before insert (check optionValue)
There was a problem hiding this 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.
Hello @orlangur |
@ravi-chandra3197 not the option value must be unique but a combination <attribute, option_label> or even <attribute, option_label, store_id>. |
@orlangur eav_attribute_option_value table only store so it's not possible to resolve issue using adding Unique index for any of above table. |
Thanks for explanation, @ravi-chandra3197, finally figured it out. Closing PR in favor of abovementioned one. |
Hi @ravi-chandra3197, thank you for your contribution! |
@orlangur |
@ravi-chandra3197 why did you reopen this PR? |
@orlangur in the backend, it uses ajax call validates unique attribute option value in an array. |
@ravi-chandra3197 could your please read my comment again? |
Hi @ravi-chandra3197, so basically #21424 solves the same issue. |
Hi @ravi-chandra3197, thank you for your contribution! |
Description (*)
Fixed Magento2 Attribute option does not validate for existing records before insert
Fixed Issues (if relevant)
Manual testing scenarios (*)
Contribution checklist (*)