/** * Retrieve max sort order * * @param \Magento\Framework\Model\AbstractModel $object * @return int */ protected function _getMaxSortOrder($object) { $adapter = $this->_getReadAdapter(); $bind = [':attribute_set_id' => $object->getAttributeSetId()]; $select = $adapter->select()->from($this->getMainTable(), new \Zend_Db_Expr("MAX(sort_order)"))->where('attribute_set_id = :attribute_set_id'); return $adapter->fetchOne($select, $bind); }
/** * Save in set including * * @param AbstractModel $object * @param null $attributeEntityId * @param null $attributeSetId * @param null $attributeGroupId * @param null $attributeSortOrder * @return $this * @SuppressWarnings(PHPMD.NPathComplexity) */ public function saveInSetIncluding(AbstractModel $object, $attributeEntityId = null, $attributeSetId = null, $attributeGroupId = null, $attributeSortOrder = null) { $attributeId = $attributeEntityId === null ? (int) $object->getId() : (int) $attributeEntityId; $setId = $attributeSetId === null ? (int) $object->getAttributeSetId() : (int) $attributeSetId; $groupId = $attributeGroupId === null ? (int) $object->getAttributeGroupId() : (int) $attributeGroupId; $attributeSortOrder = $attributeSortOrder === null ? (int) $object->getSortOrder() : (int) $attributeSortOrder; if ($setId && $groupId && $object->getEntityTypeId()) { $connection = $this->getConnection(); $table = $this->getTable('eav_entity_attribute'); $sortOrder = $attributeSortOrder ?: $this->_getMaxSortOrder($object) + 1; $data = ['entity_type_id' => $object->getEntityTypeId(), 'attribute_set_id' => $setId, 'attribute_group_id' => $groupId, 'attribute_id' => $attributeId, 'sort_order' => $sortOrder]; $where = ['attribute_id =?' => $attributeId, 'attribute_set_id =?' => $setId]; $connection->delete($table, $where); $connection->insert($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); }
/** * Save in set including * * @param AbstractModel $object * @return $this */ public function saveInSetIncluding(AbstractModel $object) { $attributeId = (int) $object->getId(); $setId = (int) $object->getAttributeSetId(); $groupId = (int) $object->getAttributeGroupId(); if ($setId && $groupId && $object->getEntityTypeId()) { $adapter = $this->_getWriteAdapter(); $table = $this->getTable('eav_entity_attribute'); $sortOrder = $object->getSortOrder() ?: $this->_getMaxSortOrder($object) + 1; $data = array('entity_type_id' => $object->getEntityTypeId(), 'attribute_set_id' => $setId, 'attribute_group_id' => $groupId, 'attribute_id' => $attributeId, 'sort_order' => $sortOrder); $where = array('attribute_id =?' => $attributeId, 'attribute_set_id =?' => $setId); $adapter->delete($table, $where); $adapter->insert($table, $data); } return $this; }