• Модуль: main
  • Путь к файлу: ~/bitrix/modules/main/lib/orm/objectify/entityobject.php
  • Класс: BitrixMainORMObjectifyEntityObject
  • Вызов: EntityObject::save
public function save()
{
	// default empty result
	switch ($this->state)
	{
		case State::RAW:
			$result = new AddResult;
			break;
		case State::CHANGED:
		case State::ACTUAL:
			$result = new UpdateResult;
			break;
		default:
			$result = new Result;
	}

	if ($this->_savingInProgress)
	{
		return $result;
	}

	$this->_savingInProgress = true;

	$dataClass = $this->entity->getDataClass();

	// check for object fields, it could be changed without notification
	foreach ($this->_currentValues as $fieldName => $currentValue)
	{
		$field = $this->entity->getField($fieldName);

		if ($field instanceof ObjectField)
		{
			$actualValue = $this->_actualValues[$fieldName];

			if ($field->encode($currentValue) !== $field->encode($actualValue))
			{
				if ($this->_state === State::ACTUAL)
				{
					// value has changed, set new state
					$this->_state = State::CHANGED;
				}
			}
			else
			{
				// value has not changed, hide it until postSave
				unset($this->_currentValues[$fieldName]);
			}
		}
	}

	// save data
	if ($this->_state == State::RAW)
	{
		$data = $this->_currentValues;
		$data['__object'] = $this;

		// put secret key __object to array
		$result = $dataClass::add($data);

		// check for error
		if (!$result->isSuccess())
		{
			$this->_savingInProgress = false;

			return $result;
		}

		// set primary
		foreach ($result->getPrimary() as $primaryName => $primaryValue)
		{
			$this->sysSetActual($primaryName, $primaryValue);

			// db value has priority in case of custom value for autocomplete
			$this->sysSetValue($primaryName, $primaryValue);
		}

		// on primary gain event
		$this->sysOnPrimarySet();
	}
	elseif ($this->_state == State::CHANGED)
	{
		// changed scalar and reference
		if (!empty($this->_currentValues))
		{
			$data = $this->_currentValues;
			$data['__object'] = $this;

			// put secret key __object to array
			$result = $dataClass::update($this->primary, $data);

			// check for error
			if (!$result->isSuccess())
			{
				$this->_savingInProgress = false;

				return $result;
			}
		}
	}

	// changed collections
	$this->sysSaveRelations($result);

	// return if there were errors
	if (!$result->isSuccess())
	{
		return $result;
	}

	$this->sysPostSave();

	$this->_savingInProgress = false;

	return $result;
}