R
R
Roman Lee2021-07-27 17:07:59
1C-Bitrix
Roman Lee, 2021-07-27 17:07:59

How to display certain sections and elements nested in them using ORM Bitrix?

The task is to display certain sections and the elements located in them using Bitrix ORM in the following form:
Section name 1

  1. Document 1
  2. Document 2

Section name N
  1. Document N
  2. Document N


Conditions - One element can be in different sections. The level of nesting of sections can be different. It is desirable to execute selection from a DB for one request.

Implementation
We use ORM Bitrix
use \Bitrix\Main\Loader;
Loader::includeModule('iblock');

$iblockId = 7; // ID Инфоблока 
$sections = array(298, 4);  // ID необходимых разделов
$elementId = 29; // ID элемента - для проверки в каких разделах состоит элемент

// API инфоблока - Docs 
// Попробуем реализовать через getList
$elements = \Bitrix\Iblock\Elements\ElementDocsTable::getList([
    // Чтобы получить все разделы элемента, в select указываем SECTIONS
    // Из SECTIONS выберем только ID, NAME и добавим алиасы SECTION_
    'select' => ['ID', 'NAME', 'IBLOCK_ID', 'SECTION_ID'=>'SECTIONS.ID', 'SECTION_NAME'=>'SECTIONS.NAME'],
    'filter' => [
        'IBLOCK_ID' => $iblockId, // ID инфоблока
        'SECTIONS.ID' => $sections, // ID необходимых разделов
        // 'ID' => $elementId, // Для проверки в каких разделах состоит элемент
    ],
    'limit' => '20', // Количество элементов
])->fetchAll();

As a result, we get the following array
One element with 'ID' => 29 consists of two sections, with different levels of nesting. The keys in the array are 0 and 2.
Array(
    [0] => Array
        (
            [ID] => 29
            [NAME] => Федеральный закон от 25.12.2008 №273-ФЗ "О противодействии коррупции"
            [IBLOCK_ID] => 7
            [SECTION_ID] => 4
            [SECTION_NAME] => Противодействие коррупции
        )
    [1] =>  Array
        (
            [ID] => 36
            [NAME] => Положение о комиссии по урегулированию споров и предотвращению конфликта интересов
            [IBLOCK_ID] => 7
            [SECTION_ID] => 4
            [SECTION_NAME] => Противодействие коррупции
        )
    [2] => Array
        (
            [ID] => 29
            [NAME] => Федеральный закон от 25.12.2008 №273-ФЗ "О противодействии коррупции"
            [IBLOCK_ID] => 7
            [SECTION_ID] => 298
            [SECTION_NAME] => Архив
        )
    [3] => Array
        (
            [ID] => 29
            [NAME] => Постановление правительства от 05.07.2013 №568 "О распространении ограничений"
            [IBLOCK_ID] => 7
            [SECTION_ID] => 298
            [SECTION_NAME] => Архив
        )
)


Question - How to form the result of $elements into an array of the following form?
It is desirable to write the implementation in the answer.
Array
(
    [0] => Array
        (
            [SECTION_ID] => 29
            [SECTION_NAME] => Раздел 1
            [ELEMENTS] => Array
                (
                    [0] => Array
                        (
                            [ID] => 11
                            [ELEMENT_NAME] => Документ 1
                        )

                    [1] => Array
                        (
                            [ID] => 223
                            [ELEMENT_NAME] => Документ 2
                        )

                )

        )

    [1] => Array
        (
            [SECTION_ID] => 30
            [SECTION_NAME] => Раздел N
            [ELEMENTS] => Array
                (
                    [0] => Array
                        (
                            [ID] => 44
                            [ELEMENT_NAME] => Документ N
                        )

                    [1] => Array
                        (
                            [ID] => 323
                            [ELEMENT_NAME] => Документ N
                        )

                )

        )
)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Edward, 2021-07-29
@Drayde

while ($element = $elements->fetch())
{
  $sectionId = $element['SECTION_ID'];

  $result[$sectionId] = [
    'SECTION_ID' => $sectionId,
    'SECTION_NAME' => $element['SECTION_NAME'],
  ];

  $result[$sectionId]['ELEMENTS'][] = [
    'ID' => $element['ID'],
    'ELEMENT_NAME' => $element['NAME'],
  ];
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question