/** * 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(); $entityId = $this->resolveEntityId($object->getId(), $table); /** * 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() . ' = ?' => $entityId, 'store_id <> ?' => $storeId]); } $data = new \Magento\Framework\DataObject(['attribute_id' => $attribute->getAttributeId(), 'store_id' => $storeId, $this->getLinkField() => $entityId, '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; }
/** * Retrieve Select for update Flat data * * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute * @param int $store * @param bool $hasValueField flag which require option value * @return \Magento\Framework\DB\Select */ public function getFlatUpdateSelect(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute, $store, $hasValueField = true) { $adapter = $this->_getReadAdapter(); $attributeTable = $attribute->getBackend()->getTable(); $attributeCode = $attribute->getAttributeCode(); $joinConditionTemplate = "%s.entity_id = %s.entity_id" . " AND %s.entity_type_id = " . $attribute->getEntityTypeId() . " AND %s.attribute_id = " . $attribute->getId() . " AND %s.store_id = %d"; $joinCondition = sprintf($joinConditionTemplate, 'e', 't1', 't1', 't1', 't1', \Magento\Store\Model\Store::DEFAULT_STORE_ID); if ($attribute->getFlatAddChildData()) { $joinCondition .= ' AND e.child_id = t1.entity_id'; } $valueExpr = $adapter->getCheckSql('t2.value_id > 0', 't2.value', 't1.value'); /** @var $select \Magento\Framework\DB\Select */ $select = $adapter->select()->joinLeft(['t1' => $attributeTable], $joinCondition, [])->joinLeft(['t2' => $attributeTable], sprintf($joinConditionTemplate, 't1', 't2', 't2', 't2', 't2', $store), [$attributeCode => $valueExpr]); if ($attribute->getFrontend()->getInputType() != 'multiselect' && $hasValueField) { $valueIdExpr = $adapter->getCheckSql('to2.value_id > 0', 'to2.value', 'to1.value'); $select->joinLeft(['to1' => $this->getTable('eav_attribute_option_value')], "to1.option_id = {$valueExpr} AND to1.store_id = 0", [])->joinLeft(['to2' => $this->getTable('eav_attribute_option_value')], $adapter->quoteInto("to2.option_id = {$valueExpr} AND to2.store_id = ?", $store), [$attributeCode . '_value' => $valueIdExpr]); } if ($attribute->getFlatAddChildData()) { $select->where('e.is_child = 0'); } return $select; }
/** * 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; }
/** * Retrieve Select For Flat Attribute update * * @param AbstractAttribute $attribute * @param int $storeId * @return Select */ public function getFlatUpdateSelect(AbstractAttribute $attribute, $storeId) { $connection = $this->getConnection(); $joinConditionTemplate = "%s.entity_id=%s.entity_id" . " AND %s.entity_type_id = " . $attribute->getEntityTypeId() . " AND %s.attribute_id = " . $attribute->getId() . " AND %s.store_id = %d"; $joinCondition = sprintf($joinConditionTemplate, 'e', 't1', 't1', 't1', 't1', \Magento\Store\Model\Store::DEFAULT_STORE_ID); if ($attribute->getFlatAddChildData()) { $joinCondition .= ' AND e.child_id = t1.entity_id'; } $valueExpr = $connection->getCheckSql('t2.value_id > 0', 't2.value', 't1.value'); /** @var $select Select */ $select = $connection->select()->joinLeft(['t1' => $attribute->getBackend()->getTable()], $joinCondition, [])->joinLeft(['t2' => $attribute->getBackend()->getTable()], sprintf($joinConditionTemplate, 't1', 't2', 't2', 't2', 't2', $storeId), [$attribute->getAttributeCode() => $valueExpr]); if ($attribute->getFlatAddChildData()) { $select->where("e.is_child = ?", 0); } return $select; }
/** * Insert entity attribute value * * @param \Magento\Framework\Object $object * @param AbstractAttribute $attribute * @param mixed $value * @return $this */ protected function _insertAttribute($object, $attribute, $value) { /** * save required attributes in global scope every time if store id different from default */ $storeId = (int) $this->_storeManager->getStore($object->getStoreId())->getId(); if ($this->getDefaultStoreId() != $storeId) { if ($attribute->getIsRequired() || $attribute->getIsRequiredInAdminStore()) { $table = $attribute->getBackend()->getTable(); $select = $this->_getReadAdapter()->select()->from($table)->where('entity_type_id = ?', $attribute->getEntityTypeId())->where('attribute_id = ?', $attribute->getAttributeId())->where('store_id = ?', $this->getDefaultStoreId())->where('entity_id = ?', $object->getEntityId()); $row = $this->_getReadAdapter()->fetchOne($select); if (!$row) { $data = new \Magento\Framework\Object(array('entity_type_id' => $attribute->getEntityTypeId(), 'attribute_id' => $attribute->getAttributeId(), 'store_id' => $this->getDefaultStoreId(), 'entity_id' => $object->getEntityId(), 'value' => $this->_prepareValueForSave($value, $attribute))); $bind = $this->_prepareDataForTable($data, $table); $this->_getWriteAdapter()->insertOnDuplicate($table, $bind, array('value')); } } } return $this->_saveAttributeValue($object, $attribute, $value); }
/** * Retrieve attribute input type * * @param AbstractAttribute $attribute * @return string */ public function getAttributeInputType($attribute) { $dataType = $attribute->getBackend()->getType(); $imputType = $attribute->getFrontend()->getInputType(); if ($imputType == 'select' || $imputType == 'multiselect') { return 'select'; } if ($imputType == 'boolean') { return 'yesno'; } if ($imputType == 'price') { return 'price'; } if ($dataType == 'int' || $dataType == 'decimal') { return 'number'; } if ($dataType == 'datetime') { return 'date'; } return 'string'; }
/** * 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; }
/** * Insert entity attribute value * * @param \Magento\Framework\DataObject $object * @param AbstractAttribute $attribute * @param mixed $value * @return $this */ protected function _insertAttribute($object, $attribute, $value) { /** * save required attributes in global scope every time if store id different from default */ $storeId = (int) $this->_storeManager->getStore($object->getStoreId())->getId(); if ($this->getDefaultStoreId() != $storeId) { if ($attribute->getIsRequired() || $attribute->getIsRequiredInAdminStore()) { $table = $attribute->getBackend()->getTable(); $select = $this->getConnection()->select()->from($table)->where('attribute_id = ?', $attribute->getAttributeId())->where('store_id = ?', $this->getDefaultStoreId())->where($this->getLinkField() . ' = ?', $object->getData($this->getLinkField())); $row = $this->getConnection()->fetchOne($select); if (!$row) { $data = new \Magento\Framework\DataObject(['attribute_id' => $attribute->getAttributeId(), 'store_id' => $this->getDefaultStoreId(), $this->getLinkField() => $object->getData($this->getLinkField()), 'value' => $this->_prepareValueForSave($value, $attribute)]); $bind = $this->_prepareDataForTable($data, $table); $this->getConnection()->insertOnDuplicate($table, $bind, ['value']); } } } return $this->_saveAttributeValue($object, $attribute, $value); }