Skip to content

magento/magento2#12699: Multiselect Attribute is not saved #12767

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

Merged
merged 3 commits into from
Dec 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ public function testWalkAttributes()

$code = 'test_attr';
$set = 10;
$storeId = 100;

$object = $this->createPartialMock(\Magento\Catalog\Model\Product::class, ['__wakeup']);

$object->setData('test_attr', 'test_attr');
$object->setData('attribute_set_id', $set);
$object->setData('store_id', $storeId);

$entityType = new \Magento\Framework\DataObject();
$entityType->setEntityTypeCode('test');
Expand Down
135 changes: 92 additions & 43 deletions app/code/Magento/Eav/Model/Entity/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend;
use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
use Magento\Framework\App\Config\Element;
use Magento\Framework\DataObject;
use Magento\Framework\DB\Adapter\DuplicateException;
use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Exception\LocalizedException;
Expand Down Expand Up @@ -62,6 +63,13 @@ abstract class AbstractEntity extends AbstractResource implements EntityInterfac
*/
protected $_attributesByCode = [];

/**
* Attributes stored by scope (store id and attribute set id).
*
* @var array
*/
private $attributesByScope = [];

/**
* Two-dimensional array by table name and attribute name
*
Expand Down Expand Up @@ -473,6 +481,46 @@ public function addAttribute(AbstractAttribute $attribute)
return $this;
}

/**
* Adding attribute to entity by scope.
*
* @param AbstractAttribute $attribute
* @param DataObject|null $entity
* @return $this
*/
public function addAttributeByScope(AbstractAttribute $attribute, $entity = null)
{
$suffix = $entity !== null ? $this->getAttributesCacheSuffix($entity) : '0-0';
$attributeCode = $attribute->getAttributeCode();
$this->attributesByScope[$suffix][$attributeCode] = $attribute;
return $this->addAttribute($attribute);
}

/**
* Get attributes by scope
*
* @return array
*/
private function getAttributesByScope($suffix)
{
return !empty($this->attributesByScope[$suffix])
? $this->attributesByScope[$suffix]
: $this->getAttributesByCode();
}

/**
* Get attributes cache suffix.
*
* @param DataObject $object
* @return string
*/
private function getAttributesCacheSuffix(DataObject $object)
{
$attributeSetId = $object->getAttributeSetId() ?: 0;
$storeId = $object->getStoreId() ?: 0;
return $storeId . '-' . $attributeSetId;
}

/**
* Retrieve partial load flag
*
Expand Down Expand Up @@ -506,7 +554,7 @@ public function isPartialSave($flag = null)
/**
* Retrieve configuration for all attributes
*
* @param null|\Magento\Framework\DataObject $object
* @param null|DataObject $object
* @return $this
*/
public function loadAllAttributes($object = null)
Expand Down Expand Up @@ -566,7 +614,7 @@ public function attributesCompare($firstAttribute, $secondAttribute)
/**
* Check whether the attribute is Applicable to the object
*
* @param \Magento\Framework\DataObject $object
* @param DataObject $object
* @param AbstractAttribute $attribute
* @return bool
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
Expand Down Expand Up @@ -611,7 +659,8 @@ public function walkAttributes($partMethod, array $args = [], $collectExceptionM
break;
}
$results = [];
foreach ($this->getAttributesByCode() as $attrCode => $attribute) {
$suffix = $this->getAttributesCacheSuffix($args[0]);
foreach ($this->getAttributesByScope($suffix) as $attrCode => $attribute) {
if (isset($args[0]) && is_object($args[0]) && !$this->_isApplicableAttribute($args[0], $attribute)) {
continue;
}
Expand Down Expand Up @@ -830,7 +879,7 @@ public function isAttributeStatic($attribute)
/**
* Validate all object's attributes against configuration
*
* @param \Magento\Framework\DataObject $object
* @param DataObject $object
* @throws \Magento\Eav\Model\Entity\Attribute\Exception
* @return true|array
*/
Expand All @@ -856,10 +905,10 @@ public function validate($object)
/**
* Set new increment id to object
*
* @param \Magento\Framework\DataObject $object
* @param DataObject $object
* @return $this
*/
public function setNewIncrementId(\Magento\Framework\DataObject $object)
public function setNewIncrementId(DataObject $object)
{
if ($object->getIncrementId()) {
return $this;
Expand All @@ -878,7 +927,7 @@ public function setNewIncrementId(\Magento\Framework\DataObject $object)
* Check attribute unique value
*
* @param AbstractAttribute $attribute
* @param \Magento\Framework\DataObject $object
* @param DataObject $object
* @return bool
*/
public function checkAttributeUniqueValue(AbstractAttribute $attribute, $object)
Expand Down Expand Up @@ -1051,7 +1100,7 @@ protected function _prepareLoadSelect(array $selects)
/**
* Retrieve select object for loading base entity row
*
* @param \Magento\Framework\DataObject $object
* @param DataObject $object
* @param string|int $rowId
* @return \Magento\Framework\DB\Select
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
Expand All @@ -1071,7 +1120,7 @@ protected function _getLoadRowSelect($object, $rowId)
/**
* Retrieve select object for loading entity attributes values
*
* @param \Magento\Framework\DataObject $object
* @param DataObject $object
* @param string $table
* @return \Magento\Framework\DB\Select
*/
Expand All @@ -1091,7 +1140,7 @@ protected function _getLoadAttributesSelect($object, $table)
/**
* Initialize attribute value for object
*
* @param \Magento\Framework\DataObject $object
* @param DataObject $object
* @param array $valueRow
* @return $this
*/
Expand Down Expand Up @@ -1181,8 +1230,8 @@ protected function processSave($object)
/**
* Retrieve Object instance with original data
*
* @param \Magento\Framework\DataObject $object
* @return \Magento\Framework\DataObject
* @param DataObject $object
* @return DataObject
*/
protected function _getOrigObject($object)
{
Expand Down Expand Up @@ -1422,7 +1471,7 @@ protected function _processSaveData($saveData)
/**
* Process base row
*/
$entityObject = new \Magento\Framework\DataObject($entityRow);
$entityObject = new DataObject($entityRow);
$entityRow = $this->_prepareDataForTable($entityObject, $entityTable);
if ($insertEntity) {
if (!empty($entityId)) {
Expand Down Expand Up @@ -1477,7 +1526,7 @@ protected function _processSaveData($saveData)
/**
* Insert entity attribute value
*
* @param \Magento\Framework\DataObject $object
* @param DataObject $object
* @param AbstractAttribute $attribute
* @param mixed $value
* @return $this
Expand All @@ -1490,7 +1539,7 @@ protected function _insertAttribute($object, $attribute, $value)
/**
* Update entity attribute value
*
* @param \Magento\Framework\DataObject $object
* @param DataObject $object
* @param AbstractAttribute $attribute
* @param mixed $valueId
* @param mixed $value
Expand Down Expand Up @@ -1585,10 +1634,10 @@ protected function _prepareValueForSave($value, AbstractAttribute $attribute)
/**
* Delete entity attribute values
*
* @param \Magento\Framework\DataObject $object
* @param DataObject $object
* @param string $table
* @param array $info
* @return \Magento\Framework\DataObject
* @return DataObject
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
protected function _deleteAttributes($object, $table, $info)
Expand All @@ -1614,13 +1663,13 @@ protected function _deleteAttributes($object, $table, $info)
/**
* Save attribute
*
* @param \Magento\Framework\DataObject $object
* @param DataObject $object
* @param string $attributeCode
* @return $this
* @throws \Exception
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function saveAttribute(\Magento\Framework\DataObject $object, $attributeCode)
public function saveAttribute(DataObject $object, $attributeCode)
{
$attribute = $this->getAttribute($attributeCode);
$backend = $attribute->getBackend();
Expand Down Expand Up @@ -1666,8 +1715,8 @@ public function saveAttribute(\Magento\Framework\DataObject $object, $attributeC
/**
* Return attribute row to prepare where statement
*
* @param \Magento\Framework\DataObject $entity
* @param \Magento\Framework\DataObject $object
* @param DataObject $entity
* @param DataObject $object
* @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute
* @return array
*/
Expand All @@ -1688,7 +1737,7 @@ protected function getAttributeRow($entity, $object, $attribute)
/**
* Delete entity using current object's data
*
* @param \Magento\Framework\DataObject|int|string $object
* @param DataObject|int|string $object
* @return $this
* @throws \Exception
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
Expand Down Expand Up @@ -1730,7 +1779,7 @@ public function delete($object)
/**
* Evaluate Delete operations
*
* @param \Magento\Framework\DataObject|int|string $object
* @param DataObject|int|string $object
* @param string|int $id
* @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
* @return void
Expand Down Expand Up @@ -1764,10 +1813,10 @@ protected function evaluateDelete($object, $id, $connection)
/**
* After Load Entity process
*
* @param \Magento\Framework\DataObject $object
* @param DataObject $object
* @return $this
*/
protected function _afterLoad(\Magento\Framework\DataObject $object)
protected function _afterLoad(DataObject $object)
{
\Magento\Framework\Profiler::start('after_load');
$this->walkAttributes('backend/afterLoad', [$object]);
Expand All @@ -1778,10 +1827,10 @@ protected function _afterLoad(\Magento\Framework\DataObject $object)
/**
* Before delete Entity process
*
* @param \Magento\Framework\DataObject $object
* @param DataObject $object
* @return $this
*/
protected function _beforeSave(\Magento\Framework\DataObject $object)
protected function _beforeSave(DataObject $object)
{
$this->walkAttributes('backend/beforeSave', [$object]);
return $this;
Expand All @@ -1790,10 +1839,10 @@ protected function _beforeSave(\Magento\Framework\DataObject $object)
/**
* After Save Entity process
*
* @param \Magento\Framework\DataObject $object
* @param DataObject $object
* @return $this
*/
protected function _afterSave(\Magento\Framework\DataObject $object)
protected function _afterSave(DataObject $object)
{
$this->walkAttributes('backend/afterSave', [$object]);
return $this;
Expand All @@ -1802,10 +1851,10 @@ protected function _afterSave(\Magento\Framework\DataObject $object)
/**
* Before Delete Entity process
*
* @param \Magento\Framework\DataObject $object
* @param DataObject $object
* @return $this
*/
protected function _beforeDelete(\Magento\Framework\DataObject $object)
protected function _beforeDelete(DataObject $object)
{
$this->walkAttributes('backend/beforeDelete', [$object]);
return $this;
Expand All @@ -1814,10 +1863,10 @@ protected function _beforeDelete(\Magento\Framework\DataObject $object)
/**
* After delete entity process
*
* @param \Magento\Framework\DataObject $object
* @param DataObject $object
* @return $this
*/
protected function _afterDelete(\Magento\Framework\DataObject $object)
protected function _afterDelete(DataObject $object)
{
$this->walkAttributes('backend/afterDelete', [$object]);
return $this;
Expand Down Expand Up @@ -1887,54 +1936,54 @@ protected function getAttributeLoader()
/**
* Perform actions after entity load
*
* @param \Magento\Framework\DataObject $object
* @param DataObject $object
* @since 100.1.0
*/
public function afterLoad(\Magento\Framework\DataObject $object)
public function afterLoad(DataObject $object)
{
$this->_afterLoad($object);
}

/**
* Perform actions before entity save
*
* @param \Magento\Framework\DataObject $object
* @param DataObject $object
* @since 100.1.0
*/
public function beforeSave(\Magento\Framework\DataObject $object)
public function beforeSave(DataObject $object)
{
$this->_beforeSave($object);
}

/**
* Perform actions after entity save
*
* @param \Magento\Framework\DataObject $object
* @param DataObject $object
* @since 100.1.0
*/
public function afterSave(\Magento\Framework\DataObject $object)
public function afterSave(DataObject $object)
{
$this->_afterSave($object);
}

/**
* Perform actions before entity delete
*
* @param \Magento\Framework\DataObject $object
* @param DataObject $object
* @since 100.1.0
*/
public function beforeDelete(\Magento\Framework\DataObject $object)
public function beforeDelete(DataObject $object)
{
$this->_beforeDelete($object);
}

/**
* Perform actions after entity delete
*
* @param \Magento\Framework\DataObject $object
* @param DataObject $object
* @since 100.1.0
*/
public function afterDelete(\Magento\Framework\DataObject $object)
public function afterDelete(DataObject $object)
{
$this->_afterDelete($object);
}
Expand Down
Loading