A
A
Alexander2019-08-13 15:34:54
1C-Bitrix
Alexander, 2019-08-13 15:34:54

All elements from nested sections on D7?

There is a section in which there are more sections with elements. You need to display all elements by the ID of the parent element on D7.

$dbItems = \Bitrix\Iblock\ElementTable::getList(array(
  'select' => array('ID', 'NAME', 'IBLOCK_ID'),
  'filter' => array('IBLOCK_ID' => 8, 'IBLOCK_SECTION_ID ' => 5)
));

It does not work with the parent ID, if you change 5 to 17 (this is the child ID), then the elements will be displayed from the section with ID 17

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
serginhold, 2019-08-13
@serginhold

google what nested sets are, figure out how to get a section and all children, write a query in pure sql to b_iblock_section_element, join elements by IBLOCK_ELEMENT_ID to the result,
as you figure it out, transfer your query to orm D7

N
Nikita, 2020-07-31
@iFunction

So it is possible:

$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'],
    ],
]);

More like this:
$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;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question