Так можно:
$sectionId = 5;
$iblockId = 8;
$section = \Bitrix\Iblock\SectionTable::getByPrimary($sectionId, [
'filter' => ['IBLOCK_ID' => $iblockId],
'select' => ['LEFT_MARGIN', 'RIGHT_MARGIN'],
])->fetch();
$dbItems = \Bitrix\Iblock\ElementTable::getList([
'select' => ['ID', 'NAME', 'IBLOCK_ID'],
'filter' => [
'IBLOCK_ID' => $iblockId,
'>=IBLOCK_SECTION.LEFT_MARGIN' => $section['LEFT_MARGIN'],
'<=IBLOCK_SECTION.RIGHT_MARGIN' => $section['RIGHT_MARGIN'],
],
]);
Еще так:
$sectionId = 5;
$iblockId = 8;
$sectionsIds = getSubsections($sectionId, $iblockId);
$dbItems = \Bitrix\Iblock\ElementTable::getList([
'select' => ['ID', 'NAME', 'IBLOCK_ID'],
'filter' => [
'IBLOCK_ID' => $iblockId,
'IBLOCK_SECTION_ID' => $sectionsIds,
],
]);
function getSubsections(int $sectionId, int $iblockId): array
{
$sectionsIds = [];
$connection = \Bitrix\Main\Application::getConnection();
$sql = sprintf('SELECT cs.ID FROM %1$s AS ps
INNER JOIN %1$s AS cs
ON ps.LEFT_MARGIN <= cs.LEFT_MARGIN AND ps.RIGHT_MARGIN >= cs.RIGHT_MARGIN AND ps.IBLOCK_ID = cs.IBLOCK_ID
WHERE ps.ID = %2$d AND ps.IBLOCK_ID = %3$d',
\Bitrix\Iblock\SectionTable::getTableName(),
$sectionId,
$iblockId
);
$result = $connection->query($sql);
while ($section = $result->fetch()) {
$sectionsId[] = $section['ID'];
}
return $sectionsIds;
}