public function testUnsetData() { $data = ['key1' => 'value1', 'key2' => 'value2']; $this->_model->setData($data); // unset one locked $this->_model->lockAttribute('key1')->unsetData('key1'); $this->assertEquals($data, $this->_model->getData()); // unset all with read only $this->_model->setIsReadonly(true)->unsetData(); $this->assertEquals($data, $this->_model->getData()); // unset all $this->_model->unlockAttributes()->setIsReadonly(false)->unsetData(); $this->assertEquals([], $this->_model->getData()); }
/** * @param string $entityType * @param \Magento\Catalog\Model\AbstractModel $entity * @param int $storeId * @throws \Magento\Framework\Exception\LocalizedException * @return void */ private function initAttributeValues($entityType, $entity, $storeId) { $metadata = $this->metadataPool->getMetadata($entityType); /** @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute */ $attributeTables = []; if ($metadata->getEavEntityType()) { foreach ($this->getAttributes($entityType) as $attribute) { if (!$attribute->isStatic()) { $attributeTables[$attribute->getBackend()->getTable()][] = $attribute->getAttributeId(); } } $storeIds = [Store::DEFAULT_STORE_ID]; if ($storeId !== Store::DEFAULT_STORE_ID) { $storeIds[] = $storeId; } $selects = []; foreach ($attributeTables as $attributeTable => $attributeCodes) { $select = $metadata->getEntityConnection()->select()->from(['t' => $attributeTable], ['value' => 't.value', 'store_id' => 't.store_id'])->join(['a' => $this->resourceConnection->getTableName('eav_attribute')], 'a.attribute_id = t.attribute_id', ['attribute_code' => 'a.attribute_code'])->where($metadata->getLinkField() . ' = ?', $entity->getData($metadata->getLinkField()))->where('t.attribute_id IN (?)', $attributeCodes)->where('t.store_id IN (?)', $storeIds); $selects[] = $select; } $unionSelect = new \Magento\Framework\DB\Sql\UnionExpression($selects, \Magento\Framework\DB\Select::SQL_UNION_ALL); $attributes = $metadata->getEntityConnection()->fetchAll((string) $unionSelect); foreach ($attributes as $attribute) { $this->attributesValues[$attribute['store_id']][$attribute['attribute_code']] = $attribute['value']; } } }
/** * Insert or Update attribute data * * @param \Magento\Catalog\Model\AbstractModel $object * @param AbstractAttribute $attribute * @param mixed $value * @return $this */ protected function _saveAttributeValue($object, $attribute, $value) { $connection = $this->getConnection(); $storeId = (int) $this->_storeManager->getStore($object->getStoreId())->getId(); $table = $attribute->getBackend()->getTable(); /** * If we work in single store mode all values should be saved just * for default store id * In this case we clear all not default values */ if ($this->_storeManager->hasSingleStore()) { $storeId = $this->getDefaultStoreId(); $connection->delete($table, ['attribute_id = ?' => $attribute->getAttributeId(), $this->getLinkField() . ' = ?' => $object->getId(), 'store_id <> ?' => $storeId]); } $data = new \Magento\Framework\DataObject(['attribute_id' => $attribute->getAttributeId(), 'store_id' => $storeId, $this->getLinkField() => $object->getData($this->getLinkField()), 'value' => $this->_prepareValueForSave($value, $attribute)]); $bind = $this->_prepareDataForTable($data, $table); if ($attribute->isScopeStore()) { /** * Update attribute value for store */ $this->_attributeValuesToSave[$table][] = $bind; } elseif ($attribute->isScopeWebsite() && $storeId != $this->getDefaultStoreId()) { /** * Update attribute value for website */ $storeIds = $this->_storeManager->getStore($storeId)->getWebsite()->getStoreIds(true); foreach ($storeIds as $storeId) { $bind['store_id'] = (int) $storeId; $this->_attributeValuesToSave[$table][] = $bind; } } else { /** * Update global attribute value */ $bind['store_id'] = $this->getDefaultStoreId(); $this->_attributeValuesToSave[$table][] = $bind; } return $this; }