/** * Load additional attribute data. * Load label of current active store * * @param EntityAttribute|AbstractModel $object * @return $this */ protected function _afterLoad(AbstractModel $object) { /** @var $entityType \Magento\Eav\Model\Entity\Type */ $entityType = $object->getData('entity_type'); if ($entityType) { $additionalTable = $entityType->getAdditionalAttributeTable(); } else { $additionalTable = $this->getAdditionalAttributeTable($object->getEntityTypeId()); } if ($additionalTable) { $connection = $this->getConnection(); $bind = [':attribute_id' => $object->getId()]; $select = $connection->select()->from($this->getTable($additionalTable))->where('attribute_id = :attribute_id'); $result = $connection->fetchRow($select, $bind); if ($result) { $object->addData($result); } } return $this; }
/** * Save entity attribute value * * Collect for mass save * * @param \Magento\Framework\Model\AbstractModel $object * @param AbstractAttribute $attribute * @param mixed $value * @return $this */ protected function _saveAttribute($object, $attribute, $value) { $table = $attribute->getBackend()->getTable(); if (!isset($this->_attributeValuesToSave[$table])) { $this->_attributeValuesToSave[$table] = array(); } $entityIdField = $attribute->getBackend()->getEntityIdField(); $data = array('entity_type_id' => $object->getEntityTypeId(), $entityIdField => $object->getId(), 'attribute_id' => $attribute->getId(), 'value' => $this->_prepareValueForSave($value, $attribute)); $this->_attributeValuesToSave[$table][] = $data; return $this; }
/** * Clear useless attribute values * * @param \Magento\Framework\Model\AbstractModel $object * @return $this */ protected function _clearUselessAttributeValues(\Magento\Framework\Model\AbstractModel $object) { $origData = $object->getOrigData(); if ($object->isScopeGlobal() && isset($origData['is_global']) && \Magento\Catalog\Model\Resource\Eav\Attribute::SCOPE_GLOBAL != $origData['is_global']) { $attributeStoreIds = array_keys($this->_storeManager->getStores()); if (!empty($attributeStoreIds)) { $delCondition = array('entity_type_id=?' => $object->getEntityTypeId(), 'attribute_id = ?' => $object->getId(), 'store_id IN(?)' => $attributeStoreIds); $this->_getWriteAdapter()->delete($object->getBackendTable(), $delCondition); } } return $this; }
/** * Set entity instance * * @param \Magento\Framework\Model\AbstractModel $entity * @return $this */ public function setEntity(\Magento\Framework\Model\AbstractModel $entity) { $this->_entity = $entity; if ($entity->getEntityTypeId()) { $this->setEntityType($entity->getEntityTypeId()); } return $this; }
/** * Save entity attribute value * * Collect for mass save * * @param \Magento\Framework\Model\AbstractModel $object * @param AbstractAttribute $attribute * @param mixed $value * @return $this */ protected function _saveAttribute($object, $attribute, $value) { $table = $attribute->getBackend()->getTable(); if (!isset($this->_attributeValuesToSave[$table])) { $this->_attributeValuesToSave[$table] = []; } $entityIdField = $attribute->getBackend()->getEntityIdField(); $data = [$entityIdField => $object->getId(), 'attribute_id' => $attribute->getId(), 'value' => $this->_prepareValueForSave($value, $attribute)]; if (!$this->getEntityTable() || $this->getEntityTable() == \Magento\Eav\Model\Entity::DEFAULT_ENTITY_TABLE) { $data['entity_type_id'] = $object->getEntityTypeId(); } $this->_attributeValuesToSave[$table][] = $data; return $this; }
/** * Perform actions before object delete * * @param \Magento\Framework\Model\AbstractModel $object * @return $this * @throws \Magento\Framework\Exception\StateException * @throws \Magento\Framework\Exception\LocalizedException */ protected function _beforeDelete(\Magento\Framework\Model\AbstractModel $object) { /** @var \Magento\Eav\Api\Data\AttributeSetInterface $object */ $defaultAttributeSetId = $this->eavConfig->getEntityType($object->getEntityTypeId())->getDefaultAttributeSetId(); if ($object->getAttributeSetId() == $defaultAttributeSetId) { throw new \Magento\Framework\Exception\StateException(__('Default attribute set can not be deleted')); } return parent::_beforeDelete($object); }
/** * Delete entity * * @param \Magento\Framework\Model\AbstractModel $object * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ public function deleteEntity(\Magento\Framework\Model\AbstractModel $object) { if (!$object->getEntityAttributeId()) { return $this; } $result = $this->getEntityAttribute($object->getEntityAttributeId()); if ($result) { $attribute = $this->_eavConfig->getAttribute($object->getEntityTypeId(), $result['attribute_id']); try { $this->attrLockValidator->validate($attribute, $result['attribute_set_id']); } catch (\Magento\Framework\Exception\LocalizedException $exception) { throw new \Magento\Framework\Exception\LocalizedException(__('Attribute \'%1\' is locked. %2', $attribute->getAttributeCode(), $exception->getMessage())); } $backendTable = $attribute->getBackend()->getTable(); if ($backendTable) { $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); $select = $this->getConnection()->select()->from($attribute->getEntity()->getEntityTable(), $linkField)->where('attribute_set_id = ?', $result['attribute_set_id']); $clearCondition = ['attribute_id =?' => $attribute->getId(), $linkField . ' IN (?)' => $select]; $this->getConnection()->delete($backendTable, $clearCondition); } } $condition = ['entity_attribute_id = ?' => $object->getEntityAttributeId()]; $this->getConnection()->delete($this->getTable('eav_entity_attribute'), $condition); return $this; }