Skip to content

Commit

Permalink
MAGETWO-83205: Check attribute unique between same fields in magento …
Browse files Browse the repository at this point in the history
…commerce [backport 2.1] #11621
  • Loading branch information
ishakhsuvarov authored Nov 6, 2017
2 parents 93f7e25 + b96523e commit 77a6e29
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions app/code/Magento/Eav/Model/Entity/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -928,28 +928,33 @@ public function checkAttributeUniqueValue(AbstractAttribute $attribute, $object)
{
$connection = $this->getConnection();
$select = $connection->select();
if ($attribute->getBackend()->getType() === 'static') {

$entityIdField = $this->getEntityIdField();
$attributeBackend = $attribute->getBackend();
if ($attributeBackend->getType() === 'static') {
$value = $object->getData($attribute->getAttributeCode());
$bind = ['value' => trim($value)];

$select->from(
$this->getEntityTable(),
$this->getEntityIdField()
$entityIdField
)->where(
$attribute->getAttributeCode() . ' = :value'
);
} else {
$value = $object->getData($attribute->getAttributeCode());
if ($attribute->getBackend()->getType() == 'datetime') {
if ($attributeBackend->getType() == 'datetime') {
$value = (new \DateTime($value))->format('Y-m-d H:i:s');
}
$bind = [
'attribute_id' => $attribute->getId(),
'value' => trim($value),
];

$entityIdField = $object->getResource()->getLinkField();
$select->from(
$attribute->getBackend()->getTable(),
$object->getResource()->getLinkField()
$attributeBackend->getTable(),
$entityIdField
)->where(
'attribute_id = :attribute_id'
)->where(
Expand All @@ -964,9 +969,10 @@ public function checkAttributeUniqueValue(AbstractAttribute $attribute, $object)

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

if ($object->getId()) {
$objectId = $object->getData($entityIdField);
if ($objectId) {
if (isset($data[0])) {
return $data[0] == $object->getId();
return $data[0] == $objectId;
}
return true;
}
Expand Down

0 comments on commit 77a6e29

Please sign in to comment.