1C-Bitrix. Core d7 in working with information block elements

<?php
$iblockId = 12;
\CIBlockElement::getList(
    ['ID' => 'DESC'], // правила сортировка
    [
        '>=DATE_ACTIVE_FROM' => '29.11.2023', 
        'IBLOCK_ID' => $iblockId
    ], // фильтр
    false, // правила группировки
    false, // параметры постраничной навигации
    ['PROPERTY_*', '*'], // выборка
);

The block above describes the method signature for working with information block elements in Bitrix. At first glance, it has its advantages, it is documented, there are many articles about it on forums. Its debugging has already been discussed in detail during the existence of the CMS. I note that there are a number of problems for which it is not suitable. In the current realities of development, even a novice programmer will encounter this function quite quickly.

Fortunately, the 1C-Bitrix developers stubbornly continue to cut apart the monolithic structures that they began to write at the turn of the millennium. Now a programmer has at least 2 different ways of working with inofblock elements in the d7 core. Let’s look at the first one (it’s bad, I’ll tell you why below, you can go straight to the second one).

Heirs of DataManager in 1C-Bitrix

<?php
use Bitrix\Main\Loader;
use Bitrix\Iblock\ElementTable;

Loader::includeModule('iblock');

$iblockId = 12;
$rsElements = ElementTable::getList([
    'filter' => [
        '>=ACTIVE_FROM' => '29.11.2023',
        'IBLOCK_ID' => $iblockId,
    ],
    'select' => [
        'ID',
        'NAME',
    ],
]);

Let me draw your attention to the fact that the ElementTable class and the getList method have documentation. Unfortunately, you won’t be able to get the properties of an information block this way. As you know, 1C-Bitrix could not solve the problem of separate storage of properties and fields for a long time. This method, in turn, is more suitable for a quick selection of identifiers and will work faster than \CIBlockElement::getList. To obtain properties using this method you will have to rack your brain. Using the runtime key you can add magic, I found an example here.

ORM in 1C-Bitrix

Unfortunately, this won’t work, let’s look at another example. First, let’s go to the settings of the information block we are interested in. Secondly, fill in the “API Symbolic Code” field.

Information block settings "State history"

Settings of the information block “State history”

Next we will use the class \Bitrix\Iblock\Elements\Element*****Table, where ***** is the “Character API code”.

<?php
use Bitrix\Main\Loader;

Loader::includeModule('iblock');

$entityClass = "\Bitrix\Iblock\Elements\ElementStateHistoryS1Table";
// символы между Element и Table регистронезависимы
$elementsCollection = $entityClass::getList([
    'filter' => [
        '>=ACTIVE_FROM' => '29.11.2023'
    ],
    'select' => [
        'ID',
        'NAME',
        'STATE', // свойство инфоблока История состояний в Битрикс24
        'IBLOCK_SECTION',
    ],
])->fetchCollection();

foreach ($elementsCollection as $elementObject) {
    #var_dump($elementObject) не сработает, можете проверить (это не массив)
    $name = $elementObject->getName(); # или $elementObject['NAME']
    $id = $elementObject->getId(); # или $elementObject['ID']
    $state = $elementObject->getState()->getValue(); # у по другому
    $iblockSectionId = $elementObject->getIblockSection()->getName();
}

The fetchCollection() method allows you to retrieve properties with correctly named keys. In turn, I advise you to try to run this block of code yourself using the fetchAll() method, everything will not be so rosy. To receive we can use the key [‘STATE’] or the getState() method. For correct use of methods it is used CamelCase. You can also get fields using the ->get(‘ID’) method. $iblockSectionId->getIblockSection()->getName() gets the name of the infoblock section.

Fortunately, the functionality of d7 in the iblock 1C-Bitrix module is not limited to this. For example, we can easily get the sections of information blocks we need by working with the section as with an object. Let’s look at an example.

<?php
use Bitrix\Main\Loader;
use Bitrix\Main\Config\Option;
use Bitrix\Iblock\Model\Section;

Loader::includeModule('iblock');
$structureIblockId = Option::get('intranet', 'iblock_structure', 3);
$orgStructureEntity = Section::compileEntityByIblock($structureIblockId);

$rsDepartments = $orgStructureEntity::getList([
    'filter' => [
          'UF_HEAD' => 9,
     ],
     'select' => ['ID', 'NAME'],
])->fetchAll();

$managerDepartmentNames="";
foreach ($rsDepartments as $dep) {
    $managerDepartmentNames .= $dep['NAME'] . ' ';
}

Note that the code above contains several plugin classes at once. We’ll talk about this in the next section of the article. In this piece of code, I get the names of all departments in which the head is a user with ID 9. Please note that this code refers to the Bitrix24 product from 1C-Bitrix. So, using the method Section::compileEntityByIblock() we get a class for working with sections of the information block of the organizational structure. Let me remind you that we did something similar above, when we created the symbolic API code for the infoblock and wrote its class directly in the code. In turn, this method is used to work with partitions. Let me draw your attention to the fact that the keys of the array passed to getList are the same as those of the elements; filtering by additional fields is easy, as is getting them (add to the array [‘ID’, ‘NAME’, ‘UF_HEAD’]).

useful links

The Option class is very well written about in official documentation. Also about the DataManager class we read there.

Conclusion

I will try to write more articles about the d7 core. I will probably tell you more about query caching (and caching in 1C-Bitrix in general).

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *