/** * 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; }
/** * Init indexing process after category delete * * @return \Magento\Framework\Model\AbstractModel */ public function afterDeleteCommit() { $this->reindex(); return parent::afterDeleteCommit(); }
/** * Sets product image from it's child if possible * * @return string */ public function getImage() { $this->getTypeInstance()->setImageFromChildProduct($this); return parent::getImage(); }
/** * Update attribute value for specific store * * @param \Magento\Catalog\Model\AbstractModel $object * @param object $attribute * @param mixed $value * @param int $storeId * @return $this */ protected function _updateAttributeForStore($object, $attribute, $value, $storeId) { $adapter = $this->_getWriteAdapter(); $table = $attribute->getBackend()->getTable(); $entityIdField = $attribute->getBackend()->getEntityIdField(); $select = $adapter->select()->from($table, 'value_id')->where('entity_type_id = :entity_type_id')->where("{$entityIdField} = :entity_field_id")->where('store_id = :store_id')->where('attribute_id = :attribute_id'); $bind = array('entity_type_id' => $object->getEntityTypeId(), 'entity_field_id' => $object->getId(), 'store_id' => $storeId, 'attribute_id' => $attribute->getId()); $valueId = $adapter->fetchOne($select, $bind); /** * When value for store exist */ if ($valueId) { $bind = array('value' => $this->_prepareValueForSave($value, $attribute)); $where = array('value_id = ?' => (int) $valueId); $adapter->update($table, $bind, $where); } else { $bind = array($entityIdField => (int) $object->getId(), 'entity_type_id' => (int) $object->getEntityTypeId(), 'attribute_id' => (int) $attribute->getId(), 'value' => $this->_prepareValueForSave($value, $attribute), 'store_id' => (int) $storeId); $adapter->insert($table, $bind); } return $this; }
/** * @param string $entityType * @param \Magento\Catalog\Model\AbstractModel $entity * @param int $storeId * @throws \Magento\Framework\Exception\LocalizedException * @return void */ private function initAttributeValues($entityType, $entity, $storeId) { $metadata = $this->metadataPool->getMetadata($entityType); /** @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute */ $attributeTables = []; if ($metadata->getEavEntityType()) { foreach ($this->getAttributes($entityType) as $attribute) { if (!$attribute->isStatic()) { $attributeTables[$attribute->getBackend()->getTable()][] = $attribute->getAttributeId(); } } $storeIds = [Store::DEFAULT_STORE_ID]; if ($storeId !== Store::DEFAULT_STORE_ID) { $storeIds[] = $storeId; } $selects = []; foreach ($attributeTables as $attributeTable => $attributeCodes) { $select = $metadata->getEntityConnection()->select()->from(['t' => $attributeTable], ['value' => 't.value', 'store_id' => 't.store_id'])->join(['a' => $this->resourceConnection->getTableName('eav_attribute')], 'a.attribute_id = t.attribute_id', ['attribute_code' => 'a.attribute_code'])->where($metadata->getLinkField() . ' = ?', $entity->getId())->where('t.attribute_id IN (?)', $attributeCodes)->where('t.store_id IN (?)', $storeIds); $selects[] = $select; } $unionSelect = new \Magento\Framework\DB\Sql\UnionExpression($selects, \Magento\Framework\DB\Select::SQL_UNION_ALL); $attributes = $metadata->getEntityConnection()->fetchAll((string) $unionSelect); foreach ($attributes as $attribute) { $this->attributesValues[$attribute['store_id']][$attribute['attribute_code']] = $attribute['value']; } } }
/** * Init indexing process after category delete * * @return \Magento\Framework\Model\AbstractModel */ protected function _afterDeleteCommit() { $this->reindex(); return parent::_afterDeleteCommit(); }
/** * @covers \Magento\Catalog\Model\AbstractModel::isReadonly * @covers \Magento\Catalog\Model\AbstractModel::setIsReadonly */ public function testIsReadonly() { $this->assertFalse($this->_model->isReadonly()); $this->_model->setIsReadonly(true); $this->assertTrue($this->_model->isReadonly()); }
/** * Update attribute value for specific store * * @param \Magento\Catalog\Model\AbstractModel $object * @param object $attribute * @param mixed $value * @param int $storeId * @return $this */ protected function _updateAttributeForStore($object, $attribute, $value, $storeId) { $connection = $this->getConnection(); $table = $attribute->getBackend()->getTable(); $entityIdField = $this->getLinkField(); $select = $connection->select()->from($table, 'value_id')->where("{$entityIdField} = :entity_field_id")->where('store_id = :store_id')->where('attribute_id = :attribute_id'); $bind = ['entity_field_id' => $object->getId(), 'store_id' => $storeId, 'attribute_id' => $attribute->getId()]; $valueId = $connection->fetchOne($select, $bind); /** * When value for store exist */ if ($valueId) { $bind = ['value' => $this->_prepareValueForSave($value, $attribute)]; $where = ['value_id = ?' => (int) $valueId]; $connection->update($table, $bind, $where); } else { $bind = [$entityIdField => (int) $object->getId(), 'attribute_id' => (int) $attribute->getId(), 'value' => $this->_prepareValueForSave($value, $attribute), 'store_id' => (int) $storeId]; $connection->insert($table, $bind); } return $this; }