Skip to content

Commit ae1d7c7

Browse files
committed
Merge remote-tracking branch 'jackalopes/MAGETWO-71708-unique-attribute' into pr-aug29
2 parents 3cac42e + 0788e64 commit ae1d7c7

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed

app/code/Magento/Eav/Model/Entity/AbstractEntity.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ public function getEntityIdField()
767767
*/
768768
public function getValueEntityIdField()
769769
{
770-
return $this->getEntityIdField();
770+
return $this->getLinkField();
771771
}
772772

773773
/**
@@ -885,28 +885,33 @@ public function checkAttributeUniqueValue(AbstractAttribute $attribute, $object)
885885
{
886886
$connection = $this->getConnection();
887887
$select = $connection->select();
888-
if ($attribute->getBackend()->getType() === 'static') {
888+
889+
$entityIdField = $this->getEntityIdField();
890+
$attributeBackend = $attribute->getBackend();
891+
if ($attributeBackend->getType() === 'static') {
889892
$value = $object->getData($attribute->getAttributeCode());
890893
$bind = ['value' => trim($value)];
891894

892895
$select->from(
893896
$this->getEntityTable(),
894-
$this->getEntityIdField()
897+
$entityIdField
895898
)->where(
896899
$attribute->getAttributeCode() . ' = :value'
897900
);
898901
} else {
899902
$value = $object->getData($attribute->getAttributeCode());
900-
if ($attribute->getBackend()->getType() == 'datetime') {
903+
if ($attributeBackend->getType() == 'datetime') {
901904
$value = (new \DateTime($value))->format('Y-m-d H:i:s');
902905
}
903906
$bind = [
904907
'attribute_id' => $attribute->getId(),
905908
'value' => trim($value),
906909
];
910+
911+
$entityIdField = $attributeBackend->getEntityIdField();
907912
$select->from(
908-
$attribute->getBackend()->getTable(),
909-
$object->getResource()->getLinkField()
913+
$attributeBackend->getTable(),
914+
$entityIdField
910915
)->where(
911916
'attribute_id = :attribute_id'
912917
)->where(
@@ -921,9 +926,10 @@ public function checkAttributeUniqueValue(AbstractAttribute $attribute, $object)
921926

922927
$data = $connection->fetchCol($select, $bind);
923928

924-
if ($object->getId()) {
929+
$objectId = $object->getData($entityIdField);
930+
if ($objectId) {
925931
if (isset($data[0])) {
926-
return $data[0] == $object->getId();
932+
return $data[0] == $objectId;
927933
}
928934
return true;
929935
}

dev/tests/integration/testsuite/Magento/Catalog/Model/ProductTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,47 @@ public function testValidateUniqueInputAttributeValue()
487487
);
488488
}
489489

490+
/**
491+
* @magentoDbIsolation enabled
492+
* @magentoDataFixture Magento/Catalog/_files/products_with_unique_input_attribute.php
493+
*/
494+
public function testValidateUniqueInputAttributeOnTheSameProduct()
495+
{
496+
/** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute */
497+
$attribute = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
498+
->get(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class)
499+
->loadByCode(\Magento\Catalog\Model\Product::ENTITY, 'unique_input_attribute');
500+
$this->_model = $this->_model->loadByAttribute(
501+
'sku',
502+
'simple product with unique input attribute'
503+
);
504+
$this->_model->setTypeId(
505+
'simple'
506+
)->setAttributeSetId(
507+
4
508+
)->setName(
509+
'Simple Product with non-unique value'
510+
)->setSku(
511+
'some product SKU'
512+
)->setPrice(
513+
10
514+
)->setMetaTitle(
515+
'meta title'
516+
)->setData(
517+
$attribute->getAttributeCode(),
518+
'unique value'
519+
)->setVisibility(
520+
\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH
521+
)->setStatus(
522+
\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED
523+
)->setCollectExceptionMessages(
524+
true
525+
);
526+
527+
$validationResult = $this->_model->validate();
528+
$this->assertTrue($validationResult);
529+
}
530+
490531
/**
491532
* @magentoDataFixture Magento/Catalog/_files/product_simple_with_custom_options.php
492533
* @magentoAppIsolation enabled

0 commit comments

Comments
 (0)