izica/bitrix-iblock-query-builder

Bitrix CMS 查询构建器

3.3.1 2023-04-09 00:53 UTC

This package is auto-updated.

Last update: 2024-09-09 04:50:12 UTC


README

通过 composer 安装(或下载),在 php_interface/init.php 中连接

composer require izica/bitrix-iblock-query-builder

使用方法

功能

  • 获取导航信息
  • 获取信息块的分区和元素
  • 可以任意顺序调用函数的箭头
  • 映射结果
  • 自动缓存
  • 自动获取元素属性,可禁用属性获取

类描述

  • IblockQuery

    • items()
    • sections()
    • filter($arFilter)
    • sort($arSort)
    • nav($arNav)
    • select($arSelect)
    • properties($bBoolean)
    • cache($sCustomKey = 'iblock-query', $ttl = 3600)
    • map($fnCallable)
    • execute() => IblockQueryResult
  • IblockQueryResult

    • all() // 返回查询结果数组
    • nav() // 分页数据
    • count()

注意

数组 $arSort, $arFilter, $arSelect, $arNav 符合格式 [https://dev.1c-bitrix.ru/api_help/iblock/classes/ciblockelement/getlist.php]

$arBadgesItems = IblockQuery::items()
    ->filter($arFilter)
    ->select($arSelect)
    ->sort($arSort)
    ->nav($arNav)
    ->properties(false) // выключает запрос на доп. свойства
    ->cache()
    ->map(function($arItem){
        $arItem['PREVIEW_PICTURE'] = CFile::GetPath($arItem['PREVIEW_PICTURE']);
        return $arItem;
    })
    ->execute(); // возвращает экземляр класса IblockQueryResult

$arBadgesSections = IblockQuery::sections()
    ->filter($arFilter)
    ->select($arSelect)
    ->sort($arSort)
    ->nav($arNav)
    ->cache()
    ->execute()
    ->all();

结果映射和自动缓存

缓存将在 map() 函数之后生效,因此对图片的查询也将只执行一次直到缓存生效。

cache($module = 'iblock-query', $ttl = 3600)

$arBadgesSections = IblockQuery::items()
    ->filter($arFilter)
    ->select($arSelect)
    ->sort($arSort)
    ->nav($arNav)
    ->properties(false)
    ->cache()
    ->map(function($arItem){
        $arItem['PREVIEW_PICTURE'] = CFile::GetPath($arItem['PREVIEW_PICTURE']);
        return $arItem;
    })
    ->execute();

获取信息块元素

$arBadgesItems = IblockQuery::items()
    ->filter(['IBLOCK_ID' => $nBadgesIblockId])
    ->select(['ID', 'NAME', 'PROPERTY_COLOR'])
    ->execute();

获取信息块分区

$arBadgesSections = IblockQuery::sections()
    ->filter(['IBLOCK_ID' => $nBadgesIblockId])
    ->select(['ID', 'NAME', 'PROPERTY_COLOR'])
    ->execute();

获取无额外属性的元素

$arBadgesItems = IblockQuery::items()
    ->filter($arFilter)
    ->select($arSelect)
    ->sort($arSort)
    ->nav($arNav)
    ->properties(false)
    ->cache()
    ->execute();