A
A
Alexey Klimov2020-04-22 19:51:32
1C-Bitrix
Alexey Klimov, 2020-04-22 19:51:32

How to get categories with the maximum nesting level in Bitrix D7?

Good afternoon!

I get a list of categories from the infoblock with the following request

$res = \Bitrix\Iblock\SectionTable::getList([
            'order' => ["LEFT_MARGIN" => "ASC"],
            'select' => [
                'ID', 'NAME', 'LEFT_MARGIN', 'RIGHT_MARGIN', 'CODE', 'UF_UDS_ID'],
            "filter" => array(
                "IBLOCK_ID" => CATALOG_IBLOCK,
                "ACTIVE" => "Y",
                "GLOBAL_ACTIVE" => "Y",
                "UF_UDS_ID" => false
            ),
        ]);


How to build a query in such a way that I would be given categories with the maximum nesting level. That is, the LEFT_MARGIN field must be less than the RIGHT_MARGIN field by 1 (in MYSQL: WHERE `LEFT_MARGIN` = `RIGHT_MARGIN` - 1)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ruslan, 2020-04-23
@shahruslan

Try like this:

$res = \Bitrix\Iblock\SectionTable::getList([
    'order' => ["LEFT_MARGIN" => "ASC"],
    'select' => [
        'ID', 'NAME', 'LEFT_MARGIN', 'RIGHT_MARGIN', 'CODE', 'UF_UDS_ID'],
    "filter" => array(
        "IBLOCK_ID" => CATALOG_IBLOCK,
        "ACTIVE" => "Y",
        "GLOBAL_ACTIVE" => "Y",
        "UF_UDS_ID" => false,
        'DIFF' => 1,
    ),
    'runtime' => [
        new ExpressionField('DIFF', '(%s - %s)', ['RIGHT_MARGIN', 'LEFT_MARGIN'])
    ],
]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question