A
A
Arkonstas2020-06-19 19:53:24
1C-Bitrix
Arkonstas, 2020-06-19 19:53:24

Bitrix, queries to the database?

There was a task to make a menu in Bitrix. In this static menu, it was necessary to grab the elements of the infoblock by the section url specified in the menu itself and display them in the submenu. I wrote such code in result_modifier.php

<? if (!defined('B_PROLOG_INCLUDED') || B_PROLOG_INCLUDED!==true) die();

$arFilter = Array('IBLOCK_ID'=>$IBLOCK_ID, 'GLOBAL_ACTIVE'=>'Y');
$db_list = CIBlockSection::GetList(Array($by=>$order), $arFilter, true);
while($ar_result = $db_list->GetNext())
{
  foreach ($arResult as $key => $arItem) {
    $test = explode('/', $arItem["LINK"]);
    if ($test[(count($test) - 2)] == $ar_result["CODE"]) {
      $arResult[$key]["CHILDREN"] = array();
      $arSelect = Array("ID", "NAME", "DETAIL_PAGE_URL");
      $arFilter = Array("IBLOCK_ID"=>$IBLOCK_ID, "ACTIVE"=>"Y", "SECTION_ID" => $ar_result["ID"]);
      $res = CIBlockElement::GetList(Array(), $arFilter, false, false, $arSelect);
      $i=0;
      while($ob = $res->GetNextElement())
      {
       $arFields = $ob->GetFields();
       	$arResult[$key]["CHILDREN"][$i]["NAME"] = $arFields["NAME"];
       	$arResult[$key]["CHILDREN"][$i]["DETAIL_PAGE_URL"] = $arFields["DETAIL_PAGE_URL"];
       	$i ++;
      }
    }
  }
}

But the senior developer said that there are 25 queries to the database and only two queries need to be made. I looked at debugging, 9 requests. I thought and thought and didn’t come up with it. How it is possible to optimize the code to reduce the number of queries to the database?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question