R
R
ragnar_ok2019-10-05 11:45:53
1C-Bitrix
ragnar_ok, 2019-10-05 11:45:53

Bitrix ORM: How to use JOIN to reduce the number of requests?

I get the elements of the infoblock and the value of their property of the "List" type.

use Bitrix\Iblock\ElementTable;
use Bitrix\Iblock\PropertyEnumerationTable;
use Bitrix\Iblock\ElementPropertyTable;

$idArray = [];
$resultObj = ElementTable::getList([
  'select' => ['ID'],
  'filter' => ['IBLOCK_ID' => 1, "IBLOCK_SECTION_ID" => 1, 'ACTIVE'=>'Y'],
]);
while ($rowArray = $resultObj->fetch()) {
  $idArray[] = $rowArray['ID'];
}

$idElementArray = [];
$resultObj = ElementPropertyTable::getList([
  'select' => ['VALUE'],
  'filter' => [
            'IBLOCK_ELEMENT_ID' => $idArray,
            'IBLOCK_PROPERTY_ID' => 1 
        ], 
]);
while ($rowArray = $resultObj->fetch()) {
  $idElementArray[] = $rowArray['VALUE'];
}

$resultObj = PropertyEnumerationTable::getList([
  'select' => ['VALUE'],
  'filter' => ['ID' => $idElementArray],
]);
while ($rowArray = $resultObj->fetch()) {
  $arResult[$rowArray['ID']] = $rowArray['VALUE'];
}

How to use JOIN in this code example to reduce the number of requests?
UPD:
In the end it turned out like this. Can't optimize?
$idArray = [];
    $resultObj = ElementTable::getList([
        'select' => ['ELEMENT_PROPERTY.VALUE'],
        'filter' => ['IBLOCK_ID' => 1, "IBLOCK_SECTION_ID" => 1, 'ACTIVE'=>'Y'],
        'runtime' => [
            new Reference(
                'ELEMENT_PROPERTY',
                ElementPropertyTable::class,
                Join::on('this.ID', 'ref.IBLOCK_ELEMENT_ID')
            ),
        ],
    ]);
    while ($rowArray = $resultObj->fetch()) {
        $idArray[] = $rowArray['IBLOCK_ELEMENT_ELEMENT_PROPERTY_VALUE'];
    }

    $resultArray = PropertyEnumerationTable::getList([
        'select' => ['VALUE'],
        'filter' => ['ID' => array_unique($idArray)],
    ])->fetchAll();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Georgy Baruchyan, 2019-10-05
@ragnar_ok

At runtime, you can pass as many references as you like.
I will give a small example, only I use non-get lists, the Query object:

$query  = \Entities\Marketing\PostingTmpTable::query()
             ->registerRuntimeField('CONTACT', [
                 'data_type' => '\Bitrix\Sender\ContactTable',
                 'reference' => [
                     '=this.CONTACT_ID' => 'ref.ID',
                 ],
//                 'join_type' => 'right'
             ])
             ->registerRuntimeField('PROPERTY', [
                'data_type' => $hlBlockEntity,
                'reference' => [
                    '=this.CONTACT.CODE' => 'ref.UF_EMAIL'
                ],
//                 'join_type' => 'right'
            ])
        ;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question