• Модуль: main
  • Путь к файлу: ~/bitrix/modules/main/lib/orm/objectify/entityobject.php
  • Класс: BitrixMainORMObjectifyEntityObject
  • Вызов: EntityObject::sysAddToCollection
public function sysAddToCollection($fieldName, $remoteObject)
{
	$fieldName = StringHelper::strtoupper($fieldName);

	/** @var OneToMany $field */
	$field = $this->entity->getField($fieldName);
	$remoteObjectClass = $field->getRefEntity()->getObjectClass();

	// validate object class
	if (!($remoteObject instanceof $remoteObjectClass))
	{
		throw new ArgumentException(sprintf(
			'Expected instance of `%s`, got `%s` instead', $remoteObjectClass, get_class($remoteObject)
		));
	}

	// initialize collection
	$collection = $this->sysGetValue($fieldName);

	if (empty($collection))
	{
		$collection = $field->getRefEntity()->createCollection();
		$this->_actualValues[$fieldName] = $collection;
	}

	if ($field instanceof OneToMany)
	{
		// set self to the object
		$remoteFieldName = $field->getRefField()->getName();
		$remoteObject->sysSetValue($remoteFieldName, $this);

		// if we don't have primary right now, repeat setter later
		if ($this->state == State::RAW)
		{
			$localObject = $this;

			$this->sysAddOnPrimarySetListener(function () use ($localObject, $remoteObject, $remoteFieldName) {
				$remoteObject->sysSetValue($remoteFieldName, $localObject);
			});
		}
	}

	/** @var Collection $collection Add to collection */
	$collection->add($remoteObject);

	// mark object as changed
	if ($this->_state == State::ACTUAL)
	{
		$this->sysChangeState(State::CHANGED);
	}
}