• Модуль: location
  • Путь к файлу: ~/bitrix/modules/location/lib/repository/location/database.php
  • Класс: BitrixLocationRepositoryLocationDatabase
  • Вызов: Database::saveParents
public function saveParents(Parents $parents): Result
{
	$result = new Result();

	if($parents->count() <= 0)
	{
		return new Result();
	}

	if($parents->getDescendant()->getId() <= 0)
	{
		throw new ArgumentNullException('descendant has not saved yet');
	}

	$data = [];
	$items = $parents->getItems();
	krsort($items);

	/**
	 * @var  int $level
	 * @var  Location $parentLocation
	 */
	foreach($items as $level => $parentLocation)
	{
		if($parentLocation->getId() <= 0)
		{
			$res = $parentLocation->save();

			if(!$res->isSuccess())
			{
				$result->addErrors($res->getErrors());
				continue;
			}
		}

		$data[] = [
			'DESCENDANT_ID' => (int)$parents->getDescendant()->getId(),
			'ANCESTOR_ID' => (int)$parentLocation->getId(),
			'LEVEL' => (int)$level,
		];
	}

	if(!empty($data))
	{
		$this->hierarchyTable::insertBatch($data);
	}

	return $result;
}