Example #1
0
 /**
  * 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;
 }