A
A
Andrey Korekhov2020-08-28 10:36:06
1C-Bitrix
Andrey Korekhov, 2020-08-28 10:36:06

How to remove one value of a multiple property in BITRIX using api?

there is a multiple property HIT
5f48b296db3a4531873327.png
Values 5f48b216da4a3518208654.png
​​In the product 5f48b3ce435a7597367627.jpeg
I can’t give the CIBlockElement::SetPropertyValues ​​function the right
way

CIBlockElement::SetPropertyValues($arResult["ID"], $arResult["IBLOCK_ID"], ['2141' => false], "HIT");

As a result, all values ​​of this property are reset.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
PetrPo, 2020-08-28
@Haotik

How trivial the task sounds...
Reality:

\Bitrix\Main\Loader::includeModule('iblock');

$propertyValuesIdByXmlId = $elementPropertyValuesById = [];

$iblockId = 17;
$elementId = 176;
$propertyCode = 'HIT';
$deletePropertyValuesXmlId = ['NEW'];

$iterator = \Bitrix\Iblock\PropertyEnumerationTable::getList([
  'select' => ['*'],
  'filter' => ['PROPERTY.IBLOCK_ID' => $iblockId, 'PROPERTY.CODE' => $propertyCode]
]);
while($row = $iterator->fetch()) {
  $propertyValuesIdByXmlId[$row['XML_ID']] = $row['ID'];
}

$dbRes = CIBlockElement::GetList([], ['IBLOCK_ID' => $iblockId, 'ID' => $elementId], false, false, ['IBLOCK_ID', 'ID', 'PROPERTY_'.$propertyCode]);
while($arRes = $dbRes->Fetch()) {
  $value = $arRes['PROPERTY_'.$propertyCode.'_VALUE'];
  $valueId = $arRes['PROPERTY_'.$propertyCode.'_ENUM_ID'];
  $elementPropertyValuesById[$valueId] = $value;
}

if($elementPropertyValuesById) {
  foreach($deletePropertyValuesXmlId as $xmlId) {
    $deletePropertyValueId = $propertyValuesIdByXmlId[$xmlId];
    
    if(isset($elementPropertyValuesById[$deletePropertyValueId])) {
      unset($elementPropertyValuesById[$deletePropertyValueId]);
    }
  }

  CIBlockElement::SetPropertyValuesEx($elementId, $iblockId, [$propertyCode => array_keys($elementPropertyValuesById)]);
}

PS everything is signed of course, but if something is not clear, ask
UPD
moved CIBlockElement::SetPropertyValuesEx above to the condition

W
wd-315, 2020-08-28
@wd-315

In your case you need to use SetPropertyValuesEx

The method saves the values ​​of all properties of the information block element. Unlike SetPropertyValues, it may not contain the full set of values. Property values ​​not specified in the PROPERTY_VALUES array will be preserved. This method is more economical in the number of database queries. The method is static.

https://dev.1c-bitrix.ru/api_help/iblock/classes/c...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question