• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/internals/customfieldscontroller.php
  • Класс: BitrixSaleInternalsCustomFieldsController
  • Вызов: CustomFieldsController::save
public function save(Entity $entity)
{
	if ($entity->getId() <= 0)
	{
		throw new MainSystemException(
			MainLocalizationLoc::getMessage('CUSTOM_FIELDS_CONTROLLER_ERROR_INCORRECT_ENTITY_ID')
		);
	}

	$dbRes = CustomFieldsTable::getList([
		'select' => ['ID', 'FIELD'],
		'filter' => [
			'=ENTITY_ID' => $entity->getId(),
			'=ENTITY_TYPE' => $entity::getRegistryEntity(),
			'=ENTITY_REGISTRY_TYPE' => $entity::getRegistryType()
		]
	]);

	$customFieldArray = [];
	while ($data = $dbRes->fetch())
	{
		$customFieldArray[$data['FIELD']] = $data;
	}

	foreach ($entity::getCustomizableFields() as $field)
	{
		if ($entity->isMarkedFieldCustom($field))
		{
			if (!isset($customFieldArray[$field]))
			{
				CustomFieldsTable::add([
					'ENTITY_ID' => $entity->getId(),
					'ENTITY_TYPE' => $entity::getRegistryEntity(),
					'ENTITY_REGISTRY_TYPE' => $entity::getRegistryType(),
					'FIELD' => $field
				]);
			}
		}
		else
		{
			if (isset($customFieldArray[$field]))
			{
				CustomFieldsTable::delete($customFieldArray[$field]['ID']);
			}
		}
	}

	return $entity;
}