Highload blocks in Bitrix24
Highload blocks allow:
Reduce system response time.
Reduce the load on the database.
Optimize the data processing process.
Main features
NoSQL and ORM support
Highload blocks use the NoSQL approach to storing and processing data. This allows you to work effectively with unstructured and semi-structured data.
ORM allows interaction with the database through object-oriented models. This way we get support for various data operations, such as creating, reading, updating and deleting, through object methods.
Customization and integration with other modules
It is possible to use highload blocks to store additional data related to CRM entities such as contacts, deals and companies.
There is also support for custom fields (for example, TagSelector
), which can be used in CRM filters and reports.
Highload blocks can be used to store data that is impractical to store in standard information blocks due to their volume or specificity.
The process of setting up highload blocks
Everything starts with the creation of a highload block:
Go to the administrative part of Bitrix24. In the menu, select the section “Content” and then “Highload blocks“. Press the button “Add highload block“. We specify the name of the block and its table in the DB, as well as the code that will be used in the code. You can also specify user groups there.
Next, you need to proceed to setting up the block. Add the necessary fields, specifying their type (line, whole number, date etc.), name and additional parameters, such as obligation fillings And uniqueness. For example, for a product directory, you can create fields “ID“, “Product Name“, “Price“, “Description” etc.
To add elements to the highload block, go to the administrative part, select the created highload block and add new records by filling in the corresponding fields. This process is similar to adding elements to infoblocks, but taking into account optimizations for high loads.
Let's look at examples of field settings for different cases:
Example code for creating and configuring highload block fields:
use Bitrix\Highloadblock as HL;
use Bitrix\Main\Entity;
$hlblock = HL\HighloadBlockTable::add(array(
'NAME' => 'ProductCatalog',
'TABLE_NAME' => 'b_product_catalog'
));
$entity = HL\HighloadBlockTable::compileEntity($hlblock);
$fields = array(
array(
'ENTITY_ID' => 'HLBLOCK_' . $hlblock['ID'],
'FIELD_NAME' => 'UF_NAME',
'USER_TYPE_ID' => 'string',
'MANDATORY' => 'Y'
),
array(
'ENTITY_ID' => 'HLBLOCK_' . $hlblock['ID'],
'FIELD_NAME' => 'UF_PRICE',
'USER_TYPE_ID' => 'double',
'MANDATORY' => 'Y'
),
array(
'ENTITY_ID' => 'HLBLOCK_' . $hlblock['ID'],
'FIELD_NAME' => 'UF_DESCRIPTION',
'USER_TYPE_ID' => 'string'
),
array(
'ENTITY_ID' => 'HLBLOCK_' . $hlblock['ID'],
'FIELD_NAME' => 'UF_DATE_ADDED',
'USER_TYPE_ID' => 'datetime'
)
);
foreach ($fields as $field) {
$result = $entity::addField($field);
if (!$result->isSuccess()) {
echo implode(', ', $result->getErrorMessages());
}
}
Importing data into highload blocks can be done via the admin panel or via API. CSV and XML formats are supported. In case of a CSV file, it is necessary to prepare a file with headers corresponding to the fields of the highload block.
Example for importing data from CSV:
$csvFile="/path/to/file.csv";
$handle = fopen($csvFile, 'r');
while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
$result = $entity::add(array(
'UF_NAME' => $data[0],
'UF_PRICE' => $data[1],
'UF_DESCRIPTION' => $data[2],
'UF_DATE_ADDED' => $data[3]
));
if (!$result->isSuccess()) {
echo implode(', ', $result->getErrorMessages());
}
}
fclose($handle);
Exporting data from highload blocks also supports CSV and XML formats. You can use standard methods of retrieving data and writing it to a file.
Example:
$csvFile="/path/to/export.csv";
$handle = fopen($csvFile, 'w');
fputcsv($handle, array('ID', 'Name', 'Price', 'Description', 'Date Added'));
$result = $entity::getList();
while ($item = $result->fetch()) {
fputcsv($handle, array(
$item['ID'],
$item['UF_NAME'],
$item['UF_PRICE'],
$item['UF_DESCRIPTION'],
$item['UF_DATE_ADDED']
));
}
fclose($handle);
Export data in RSS format can be implemented using standard PHP libraries to generate RSS feeds. This way you can post updates and news related to highload block data.
Code example:
use Bitrix\Highloadblock as HL;
use Bitrix\Main\Entity;
// подключаем необходимые модули и файлы
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");
CModule::IncludeModule('highloadblock');
// указываем ID highload-блока и получаем информацию о нём
$hlblockId = 1; // Замените на ID вашего highload-блока
$hlblock = HL\HighloadBlockTable::getById($hlblockId)->fetch();
$entity = HL\HighloadBlockTable::compileEntity($hlblock);
$entityDataClass = $entity->getDataClass();
// получаем данные из highload-блока
$rsData = $entityDataClass::getList(array(
"select" => array('ID', 'UF_NAME', 'UF_DESCRIPTION', 'UF_DATE_ADDED'),
"order" => array('UF_DATE_ADDED' => 'DESC'),
"limit" => 20
));
header('Content-Type: application/rss+xml; charset=utf-8');
echo '<?xml version="1.0" encoding="UTF-8" ?>';
echo '<rss version="2.0">';
echo '<channel>';
echo '<title>Highload-блок RSS</title>';
echo '<link>http://yourwebsite.com/</link>';
echo '<description>Новости из highload-блока</description>';
echo '<language>ru</language>';
// проходим по данным и формируем элементы RSS
while ($arItem = $rsData->fetch()) {
echo '<item>';
echo '<title>' . htmlspecialchars($arItem['UF_NAME']) . '</title>';
echo '<link>http://yourwebsite.com/item/' . $arItem['ID'] . '</link>';
echo '<description>' . htmlspecialchars($arItem['UF_DESCRIPTION']) . '</description>';
echo '<pubDate>' . date(DATE_RSS, strtotime($arItem['UF_DATE_ADDED'])) . '</pubDate>';
echo '</item>';
}
echo '</channel>';
echo '</rss>';
Using the highload block ID, we get information about it and the data that we will export to RSS format. Set the header Content-Type
to indicate to the browser that the document is an RSS feed. In the block while
we go through all the elements received from the highload block and form the corresponding RSS elements <item>
.
Highload blocks in Bitrix24, despite their power, have a number of limitations and potential problems. One of the main limitations is the volume of data and performance. Although highload blocks can work well with millions of records, the system performance can decrease as the volume of data increases. This is due to the fact that database queries can become slower, especially if they are not optimized or there is no caching.
As the volume of data increases, it may be necessary to optimize the database itself: indexing, setting up caching parameters, etc.
In conclusion, I would like to remind you about the open lesson, in which participants will discuss changing user interfaces in Bitrix24.
By the end of the lesson, you will know why you shouldn't override component templates, how to embed custom fields with custom layout, how to embed REST applications, and customize the interface with JavaScript.
The meeting will take place on July 16. If it is relevant – Sign up using the link.