/** * 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; }
/** * 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); }
/** * Delete entity * * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $object * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ public function deleteEntity(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $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->metadataPool->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; }
/** * Return increment needed for SKU uniqueness * * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute * @param Product $object * @return int */ protected function _getLastSimilarAttributeValueIncrement($attribute, $object) { $adapter = $this->getAttribute()->getEntity()->getReadConnection(); $select = $adapter->select(); $value = $object->getData($attribute->getAttributeCode()); $bind = array('entity_type_id' => $attribute->getEntityTypeId(), 'attribute_code' => trim($value) . '-%'); $select->from($this->getTable(), $attribute->getAttributeCode())->where('entity_type_id = :entity_type_id')->where($attribute->getAttributeCode() . ' LIKE :attribute_code')->order(array('entity_id DESC', $attribute->getAttributeCode() . ' ASC'))->limit(1); $data = $adapter->fetchOne($select, $bind); return abs((int) str_replace($value, '', $data)); }