Views: 490 (Data available since 06.02.2017)
Last Modified: 02.07.2014

Let us see an example of the creation of a proper component that will add a new entry to an entity. In order to write a component we use constructions of a new core D7. The Highloadblock namespace module must be connected without fail:

use Bitrix\Highloadblock as HL;

First, let us obtain data about the entity using its identifier:

$hlblock_id = $arParams['BLOCK_ID'];

$hlblock = HL\HighloadBlockTable::getById($hlblock_id)->fetch();

After that we initialize the entity class to be able to access the entity through API:

HL\HighloadBlockTable::compileEntity($hlblock);

The component to be created will receive values from a form, work with user-defined core properties, and call the add method from ORM:

if($_SERVER["REQUEST_METHOD"] == "POST" && $_POST["add_resume"] <> '' && check_bitrix_sessid())
{
	$data = array();

	$USER_FIELD_MANAGER->EditFormAddFields('HLBLOCK_'.$hlblock['ID'], $data);

	if (!$GLOBALS["USER_FIELD_MANAGER"]->CheckFields('HLBLOCK_'.$hlblock['ID'], 0, $data))
	{
		if(is_object($APPLICATION) && $APPLICATION->GetException())
		{
			$e = $APPLICATION->GetException();
			$errors[] = $e->GetString();
			$APPLICATION->ResetException();
		}
		else
		{
			$errors[] = "Unknown error.";
		}
	}

	if (empty($errors))
	{
		/** @param Bitrix\Main\Entity\AddResult $result */
		$result = ResumeTable::add($data);    // ORM

		if($result->isSuccess())
		{
			$ID = $result->getId();
			LocalRedirect("resume.php?ID=".intval($ID));
		}
		else
		{
			$errors = array_merge($errors, $result->getErrorMessages());
		}
	}
	
	if(!empty($errors))
	{
		$arResult['ERROR'] = implode("<br>", $errors);
		$arResult["bVarsFromForm"] = true; 
	}
}

Please note the way the operation result is processed in our example:

  • If add result is successful, we obtain an identifier of a new entity entry and get redirected to the page resume.php;
  • If there are errors, we obtain them using the method getErrorMessages and show them in the form.
Full Code of the Component


Courses developed by «Bitrix», Inc.