S
S
Sergey2020-02-28 13:31:07
1C-Bitrix
Sergey, 2020-02-28 13:31:07

D7. Sampling sections with element count?

Please advise how to select information block sections with a field containing the value of the number of section elements.

Here is the code, but in the empty section it shows 1 ...

$iBlockId = 10;
$sectionId = 53;

$filter = [
    'IBLOCK_ID' => $iBlockId,
    'ACTIVE' => 'Y',
    'IBLOCK_SECTION_ID' => $sectionId
];

$select = [
    'ID', 'IBLOCK_ID', 'ELEMENTS_COUNT'
];

$runtime = [
    'elements' => [
        'data_type' =>"Bitrix\Iblock\ElementTable",
        'expression' => 'COUNT(*)',
        'reference' => [
            '=this.IBLOCK_ID' => 'ref.IBLOCK_ID',
            '=this.ID' => 'ref.IBLOCK_SECTION_ID',
            '=this.ACTIVE' => 'ref.ACTIVE',
        ],
        //'join_type' => "LEFT"
    ],
    new Bitrix\Main\Entity\ExpressionField('ELEMENTS_COUNT', 'COUNT(*)')
];

$res = Bitrix\Iblock\SectionTable::getList(
    [
        'filter' => $filter,
        'select' => $select,
        'runtime' => $runtime
    ]
);

while($section = $res->fetch())
{
    \Bitrix\Main\Diag\Debug::dump($section, $section['ID']);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2020-02-28
@Firsov36

The question is closed. Found an error.
For those who are interested, it's done like this:

$iBlockId = 10;
$sectionId = 53;

$filter = [
    'IBLOCK_ID' => $iBlockId,
    'ACTIVE' => 'Y',
    'IBLOCK_SECTION_ID' => $sectionId
];

$select = [
    'ID', 'IBLOCK_ID', 'countElements'
];

$runtime = [
    'elements' => [
        'data_type' =>"Bitrix\Iblock\ElementTable",
        'reference' => [
            '=this.IBLOCK_ID' => 'ref.IBLOCK_ID',
            '=this.ID' => 'ref.IBLOCK_SECTION_ID',
            '=this.ACTIVE' => 'ref.ACTIVE',
        ],
        //'join_type' => "LEFT"
    ],
    'countElements' => [
        'data_type' => 'integer',
        'expression' => ['count(%s)', 'elements.ID']
    ]
];

$res = Bitrix\Iblock\SectionTable::getList(
    [
        'filter' => $filter,
        'select' => $select,
        'runtime' => $runtime
    ]
);

while($section = $res->fetch())
{
    \Bitrix\Main\Diag\Debug::dump($section, $section['ID']);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question