Beispiel #1
0
 /**
  * Update attribute value for an entity with a default value.
  * All existing values are erased by the new value.
  *
  * @param integer|string $entityTypeId Target entity id.
  * @param integer|string $attributeId  Target attribute id.
  * @param mixed          $value        Value to be set.
  * @param array          $excludedIds  List of categories that should not be updated during the process.
  *
  * @return void
  */
 private function updateAttributeDefaultValue($entityTypeId, $attributeId, $value, $excludedIds = [])
 {
     $setup = $this->eavSetup->getSetup();
     $entityTable = $setup->getTable($this->eavSetup->getEntityType($entityTypeId, 'entity_table'));
     $attributeTable = $this->eavSetup->getAttributeTable($entityTypeId, $attributeId);
     if (!is_int($attributeId)) {
         $attributeId = $this->eavSetup->getAttributeId($entityTypeId, $attributeId);
     }
     // Retrieve the primary key name. May differs if the staging module is activated or not.
     $linkField = $this->metadataPool->getMetadata(CategoryInterface::class)->getLinkField();
     $entitySelect = $this->getConnection()->select();
     $entitySelect->from($entityTable, [new \Zend_Db_Expr("{$attributeId} as attribute_id"), $linkField, new \Zend_Db_Expr("{$value} as value")]);
     if (!empty($excludedIds)) {
         $entitySelect->where("entity_id NOT IN(?)", $excludedIds);
     }
     $insertQuery = $this->getConnection()->insertFromSelect($entitySelect, $attributeTable, ['attribute_id', $linkField, 'value'], AdapterInterface::INSERT_ON_DUPLICATE);
     $this->getConnection()->query($insertQuery);
 }