• Модуль: disk
  • Путь к файлу: ~/bitrix/modules/disk/lib/internals/objectpath.php
  • Класс: BitrixDiskInternalsObjectPathTable
  • Вызов: ObjectPathTable::recalculateByStorage
static function recalculateByStorage($storageId)
{
	$storageId = (int)$storageId;
	if ($storageId <= 0)
	{
		throw new ArgumentOutOfRangeException('storageId');
	}

	set_time_limit(0);
	$maxInnerJoinDepth = 32;
	$currentDepth = 0;
	$emptyInsert = false;
	$connection = Application::getConnection();

	$connection->queryExecute("
		DELETE p
		FROM b_disk_object_path p
		INNER JOIN b_disk_object object ON object.ID = p.OBJECT_ID
		WHERE object.STORAGE_ID = {$storageId}
	");
	$connection->queryExecute("
		DELETE p
		FROM b_disk_object_path p
		INNER JOIN b_disk_object object_p ON object_p.ID = p.PARENT_ID
		WHERE object_p.STORAGE_ID = {$storageId}
	");

	$connection->queryExecute("
		INSERT INTO b_disk_object_path (PARENT_ID, OBJECT_ID, DEPTH_LEVEL)
		SELECT ID, ID, 0 FROM b_disk_object WHERE STORAGE_ID = {$storageId}
	");

	while($currentDepth < $maxInnerJoinDepth && !$emptyInsert)
	{
		$query = "
			INSERT INTO b_disk_object_path (OBJECT_ID, PARENT_ID, DEPTH_LEVEL)
				SELECT b.ID, t.ID, " . ($currentDepth+1) . " FROM b_disk_object t
		";

		$finalQuery = $query;
		for($i = 0;$i < $currentDepth;$i++)
		{
			$finalQuery .= " INNER JOIN b_disk_object t" . ($i+1) . " ON t" . ($i?: '') . ".ID=t" . ($i+1) . ".PARENT_ID ";

		}
		$lastJoin = " INNER JOIN b_disk_object b ON t" . ($currentDepth?:'' ) . ".ID=b.PARENT_ID ";
		$finalQuery = $finalQuery . $lastJoin . " WHERE t.STORAGE_ID = {$storageId}";

		$connection->queryExecute($finalQuery);
		$emptyInsert = $connection->getAffectedRowsCount() <= 0;

		$currentDepth++;
	}
}