Skip to content

Commit 77a6e29

Browse files
MAGETWO-83205: Check attribute unique between same fields in magento commerce [backport 2.1] #11621
2 parents 93f7e25 + b96523e commit 77a6e29

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -928,28 +928,33 @@ public function checkAttributeUniqueValue(AbstractAttribute $attribute, $object)
928928
{
929929
$connection = $this->getConnection();
930930
$select = $connection->select();
931-
if ($attribute->getBackend()->getType() === 'static') {
931+
932+
$entityIdField = $this->getEntityIdField();
933+
$attributeBackend = $attribute->getBackend();
934+
if ($attributeBackend->getType() === 'static') {
932935
$value = $object->getData($attribute->getAttributeCode());
933936
$bind = ['value' => trim($value)];
934937

935938
$select->from(
936939
$this->getEntityTable(),
937-
$this->getEntityIdField()
940+
$entityIdField
938941
)->where(
939942
$attribute->getAttributeCode() . ' = :value'
940943
);
941944
} else {
942945
$value = $object->getData($attribute->getAttributeCode());
943-
if ($attribute->getBackend()->getType() == 'datetime') {
946+
if ($attributeBackend->getType() == 'datetime') {
944947
$value = (new \DateTime($value))->format('Y-m-d H:i:s');
945948
}
946949
$bind = [
947950
'attribute_id' => $attribute->getId(),
948951
'value' => trim($value),
949952
];
953+
954+
$entityIdField = $object->getResource()->getLinkField();
950955
$select->from(
951-
$attribute->getBackend()->getTable(),
952-
$object->getResource()->getLinkField()
956+
$attributeBackend->getTable(),
957+
$entityIdField
953958
)->where(
954959
'attribute_id = :attribute_id'
955960
)->where(
@@ -964,9 +969,10 @@ public function checkAttributeUniqueValue(AbstractAttribute $attribute, $object)
964969

965970
$data = $connection->fetchCol($select, $bind);
966971

967-
if ($object->getId()) {
972+
$objectId = $object->getData($entityIdField);
973+
if ($objectId) {
968974
if (isset($data[0])) {
969-
return $data[0] == $object->getId();
975+
return $data[0] == $objectId;
970976
}
971977
return true;
972978
}

0 commit comments

Comments
 (0)