Attention! You can delete and update several file property values only within a single call, not via several, due to property value IDs are being changed simultaneously. For example:
|
---|
This method supposes that you collect all values to be deleted for a 1 file property (PROPERTY_VALUE_ID for each file) into an array. Then, deleting is performed.
<?php $obElement = \CIBlockElement::GetList( [], [ 'ID' => (int)$aElementID, 'IBLOCK_ID' => (int)$iMainIblockId ], false, false, [ 'ID', 'IBLOCK_ID', ]); if ($obFields = $obElement->GetNextElement()) { $arProperties = $obFields->GetProperties(); if (!empty($arProperties)) { /** * Collect files for deleting into an array, * group by properties */ foreach ($arProperties as $sPropCode => $arPropValues) { /** * $sPropCode - property code, searched by us for deleted file without PROPERTY_ and _VALUE, for example PHOTO * $arPropValues - PROPERTY_VALUE_ID of deleted file * $arFiles["FILE_DELETE"] - array containing file IDs for all properties to be deleted * $sValue - searched ID */ foreach ($arPropValues['VALUE'] as $iKeyValue => $sValue) { if (in_array($sValue, $arFiles["FILE_DELETE"]) && $arPropValues['PROPERTY_VALUE_ID'][$iKeyValue] > 0) { $arDeleteList[$sPropCode][$arPropValues['PROPERTY_VALUE_ID'][$iKeyValue]] = [ 'VALUE' => [ 'del' => 'Y', ] ]; } } } /** * When array for files deleting isn't empty * perform the deleting */ if (!empty($arDeleteList)) { foreach ($arDeleteList as $sPropForDelete => $arDeleteFiles) { CIBlockElement::SetPropertyValueCode( $aElementID, $sPropForDelete, $arDeleteFiles ); } } } } |
SetPropertyValues
CIBlockElement::SetPropertyValues(
int ELEMENT_ID,
int IBLOCK_ID,
array PROPERTY_VALUES,
string PROPERTY_CODE = false
);
This method saves values of all iblock element properties. Static method.
See Also
Call parameters
Parameter | Description |
---|---|
ELEMENT_ID | Element ID with property values to be set. |
IBLOCK_ID | Information block ID. |
PROPERTY_VALUES | Array with property values, matching property codes and values.
When PROPERTY_CODE is set, it must contain a single or array with all property values (multiple) for specified element. When PROPERTY_CODE is false, the PROPERTY_VALUES must be an Array("property code1"=>"property value1", ....), where "property code" - numerical or symbolic property code, "property values" - single or array of all property values (multiple). And the array PROPERTY_VALUES must contain a full set of property values for this element, i. e. if one of properties will be missing from it - all property values for this element will be deleted. This is the case for all property types, except for file. Files must be deleted via array with the parameter "del"=>"Y". When the file property type is multiple, the file will be deleted in case if the 'del' parameter is present, independently from its value. Notes:
|
PROPERTY_CODE | Update property code. When this parameter is different from false, updates only the property with this code. Optional parameter, set as false by default. |
Examples
Example 1:
<?
$ELEMENT_ID = 18; // element code
$PROPERTY_CODE = "PROP1"; // property code
$PROPERTY_VALUE = "Blue"; // property value
// Set new value for this property of this element
$dbr = CIBlockElement::GetList(array(), array("=ID"=>$ELEMENT_ID), false, false, array("ID", "IBLOCK_ID"));
if ($dbr_arr = $dbr->Fetch())
{
$IBLOCK_ID = $dbr_arr["IBLOCK_ID"];
CIBlockElement::SetPropertyValues($ELEMENT_ID, $IBLOCK_ID, $PROPERTY_VALUE, $PROPERTY_CODE);
}
?>
Example 2 (method call code for the "String" property):
$value="text"; CIBlockElement::SetPropertyValueCode("$ELEMENT_ID", "code", $value);
Example 3 (method call for "HTML/text" property):
$value="text"; CIBlockElement::SetPropertyValueCode("$ELEMENT_ID", "code", array("VALUE"=>array("TEXT"=>$value, "TYPE"=>"html")));
Example 4 (adding a description to value):
CIBlockElement::SetPropertyValues ( $PRODUCT_ID, $IBLOCK_ID, array("VALUE"=>$prop_value,"DESCRIPTION"=>$prop_description), $property_name );
Example 5 (deleting "File" property):
CIBlockElement::SetPropertyValuesEx(ELEMENT_ID, IBLOCK_ID, array(PROPERTY_ID => Array ("VALUE" => array("del" => "Y"))));
Example 6:
When a complete product details must be updated, including multiple type values (jointly with description), you can do it using a single Update call. You need to add the description (DESCRIPTION) to values (VALUE) of multiple type properties, write a numerical or symbolic property code (of multiple type) to PROPERTY_VALUES and assign array with value type as follows:
$arrFields = Array( 'PROPERTY_ID_OR_CODE' => Array( Array( "VALUE" => 'value1', "DESCRIPTION" => 'desc for value1' ), Array( "VALUE" => 'value2', "DESCRIPTION" => 'desc for value2' ) ) );
© «Bitrix24», 2001-2024