コード例 #1
0
ファイル: AttributeService.php プロジェクト: aiesh/magento2
 /**
  * Add attribute to attribute set and group
  *
  * @param int $attributeSetId
  * @param Data\Eav\AttributeSet\Attribute $data
  * @return int
  * @throws \Magento\Framework\Exception\InputException
  */
 public function addAttribute($attributeSetId, \Magento\Catalog\Service\V1\Data\Eav\AttributeSet\Attribute $data)
 {
     $attributeSet = $this->setFactory->create()->load($attributeSetId);
     if (!$attributeSet->getId()) {
         throw new InputException('Attribute set does not exist');
     }
     $setEntityType = $this->entityTypeFactory->create()->getEntityType($attributeSet->getEntityTypeId());
     if ($setEntityType->getEntityTypeCode() != \Magento\Catalog\Model\Product::ENTITY) {
         throw new InputException('Wrong attribute set id provided');
     }
     if (!$this->groupFactory->create()->load($data->getAttributeGroupId())->getId()) {
         throw new InputException('Attribute group does not exist');
     }
     $attribute = $this->attributeFactory->create();
     if (!$attribute->load($data->getAttributeId())->getId()) {
         throw new InputException('Attribute does not exist');
     }
     $attribute->setId($data->getAttributeId());
     $attribute->setEntityTypeId($setEntityType->getId());
     $attribute->setAttributeSetId($attributeSetId);
     $attribute->setAttributeGroupId($data->getAttributeGroupId());
     $attribute->setSortOrder($data->getSortOrder());
     $this->attributeResource->saveInSetIncluding($attribute);
     return $attribute->loadEntityAttributeIdBySet()->getData('entity_attribute_id');
 }
コード例 #2
0
 /**
  * {@inheritdoc}
  */
 public function deleteById($attributeId)
 {
     /** @var \Magento\Eav\Model\Entity\Attribute $attribute */
     $attribute = $this->attributeFactory->create();
     $this->eavResource->load($attribute, $attributeId);
     if (!$attribute->getAttributeId()) {
         throw new NoSuchEntityException(__('Attribute with id "%1" does not exist.', $attributeId));
     }
     $this->delete($attribute);
     return true;
 }
コード例 #3
0
 /**
  * {@inheritdoc}
  */
 public function delete($entityType, $attributeCode, $optionId)
 {
     if (empty($attributeCode)) {
         throw new InputException(__('Empty attribute code'));
     }
     $attribute = $this->attributeRepository->get($entityType, $attributeCode);
     if (!$attribute->usesSource()) {
         throw new StateException(__('Attribute %1 doesn\'t have any option', $attributeCode));
     }
     if (!$attribute->getSource()->getOptionText($optionId)) {
         throw new NoSuchEntityException(__('Attribute %1 does not contain option with Id %2', $attribute->getId(), $optionId));
     }
     $removalMarker = ['option' => ['value' => [$optionId => []], 'delete' => [$optionId => '1']]];
     $attribute->addData($removalMarker);
     try {
         $this->resourceModel->save($attribute);
     } catch (\Exception $e) {
         throw new StateException(__('Cannot save attribute %1', $attributeCode));
     }
     return true;
 }
コード例 #4
0
ファイル: Set.php プロジェクト: nja78/magento2
 /**
  * Collect data for save
  *
  * @param array $data
  * @return $this
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function organizeData($data)
 {
     $modelGroupArray = [];
     $modelAttributeArray = [];
     $attributeIds = [];
     if ($data['attributes']) {
         $ids = [];
         foreach ($data['attributes'] as $attribute) {
             $ids[] = $attribute[0];
         }
         $attributeIds = $this->_resourceAttribute->getValidAttributeIds($ids);
     }
     if ($data['groups']) {
         foreach ($data['groups'] as $group) {
             $modelGroup = $this->_attrGroupFactory->create();
             $modelGroup->setId(is_numeric($group[0]) && $group[0] > 0 ? $group[0] : null)->setAttributeGroupName($group[1])->setAttributeSetId($this->getId())->setSortOrder($group[2]);
             if ($data['attributes']) {
                 foreach ($data['attributes'] as $attribute) {
                     if ($attribute[1] == $group[0] && in_array($attribute[0], $attributeIds)) {
                         $modelAttribute = $this->_attributeFactory->create();
                         $modelAttribute->setId($attribute[0])->setAttributeGroupId($attribute[1])->setAttributeSetId($this->getId())->setEntityTypeId($this->getEntityTypeId())->setSortOrder($attribute[2]);
                         $modelAttributeArray[] = $modelAttribute;
                     }
                 }
                 $modelGroup->setAttributes($modelAttributeArray);
                 $modelAttributeArray = [];
             }
             $modelGroupArray[] = $modelGroup;
         }
         $this->setGroups($modelGroupArray);
     }
     if ($data['not_attributes']) {
         $modelAttributeArray = [];
         foreach ($data['not_attributes'] as $attributeId) {
             $modelAttribute = $this->_attributeFactory->create();
             $modelAttribute->setEntityAttributeId($attributeId);
             $modelAttributeArray[] = $modelAttribute;
         }
         $this->setRemoveAttributes($modelAttributeArray);
     }
     if ($data['removeGroups']) {
         $modelGroupArray = [];
         foreach ($data['removeGroups'] as $groupId) {
             $modelGroup = $this->_attrGroupFactory->create();
             $modelGroup->setId($groupId);
             $modelGroupArray[] = $modelGroup;
         }
         $this->setRemoveGroups($modelGroupArray);
     }
     $this->setAttributeSetName($data['attribute_set_name'])->setEntityTypeId($this->getEntityTypeId());
     return $this;
 }
コード例 #5
0
 /**
  * {@inheritdoc}
  */
 public function assign($entityTypeCode, $attributeSetId, $attributeGroupId, $attributeCode, $sortOrder)
 {
     try {
         $attributeSet = $this->setRepository->get($attributeSetId);
     } catch (NoSuchEntityException $ex) {
         throw new NoSuchEntityException(__('AttributeSet with id "%1" does not exist.', $attributeSetId));
     }
     $setEntityType = $this->entityTypeFactory->create()->getEntityType($attributeSet->getEntityTypeId());
     if ($setEntityType->getEntityTypeCode() != $entityTypeCode) {
         throw new InputException(__('Wrong attribute set id provided'));
     }
     //Check if group exists. If not - expected exception
     $attributeGroup = $this->groupRepository->get($attributeGroupId);
     if ($attributeGroup->getAttributeSetId() != $attributeSetId) {
         throw new InputException(__('Attribute group does not belong to attribute set'));
     }
     /** @var \Magento\Eav\Api\Data\AttributeInterface $attribute */
     $attribute = $this->attributeRepository->get($entityTypeCode, $attributeCode);
     $this->attributeResource->saveInSetIncluding($attribute, $attribute->getAttributeId(), $attributeSetId, $attributeGroupId, $sortOrder);
     $attribute->setAttributeSetId($attributeSetId);
     return $attribute->loadEntityAttributeIdBySet()->getData('entity_attribute_id');
 }
コード例 #6
0
ファイル: Attribute.php プロジェクト: pavelnovitsky/magento2
 /**
  * Perform actions after object save
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return $this
  */
 protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
 {
     $this->_clearUselessAttributeValues($object);
     return parent::_afterSave($object);
 }
コード例 #7
0
ファイル: Visibility.php プロジェクト: niranjanssiet/magento2
 /**
  * Retrieve Select For Flat Attribute update
  *
  * @param int $store
  * @return \Magento\Framework\DB\Select|null
  */
 public function getFlatUpdateSelect($store)
 {
     return $this->_eavEntityAttribute->getFlatUpdateSelect($this->getAttribute(), $store);
 }
コード例 #8
0
ファイル: Attribute.php プロジェクト: aiesh/magento2
 /**
  * Save attribute/form relations after attribute save
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return $this
  */
 protected function _afterSave(AbstractModel $object)
 {
     $forms = $object->getData('used_in_forms');
     $adapter = $this->_getWriteAdapter();
     if (is_array($forms)) {
         $where = array('attribute_id=?' => $object->getId());
         $adapter->delete($this->_getFormAttributeTable(), $where);
         $data = array();
         foreach ($forms as $formCode) {
             $data[] = array('form_code' => $formCode, 'attribute_id' => (int) $object->getId());
         }
         if ($data) {
             $adapter->insertMultiple($this->_getFormAttributeTable(), $data);
         }
     }
     // update sort order
     if (!$object->isObjectNew() && $object->dataHasChangedFor('sort_order')) {
         $data = array('sort_order' => $object->getSortOrder());
         $where = array('attribute_id=?' => (int) $object->getId());
         $adapter->update($this->getTable('eav_entity_attribute'), $data, $where);
     }
     // save scope attributes
     $websiteId = (int) $object->getWebsite()->getId();
     if ($websiteId) {
         $table = $this->_getEavWebsiteTable();
         $describe = $this->_getReadAdapter()->describeTable($table);
         $data = array();
         if (!$object->getScopeWebsiteId() || $object->getScopeWebsiteId() != $websiteId) {
             $data = $this->getScopeValues($object);
         }
         $data['attribute_id'] = (int) $object->getId();
         $data['website_id'] = (int) $websiteId;
         unset($describe['attribute_id']);
         unset($describe['website_id']);
         $updateColumns = array();
         foreach (array_keys($describe) as $columnName) {
             $data[$columnName] = $object->getData('scope_' . $columnName);
             $updateColumns[] = $columnName;
         }
         $adapter->insertOnDuplicate($table, $data, $updateColumns);
     }
     return parent::_afterSave($object);
 }