izica / iblock-query

此软件包已废弃且不再维护。作者建议使用https://github.com/izica/bitrix-iblock-query-builder软件包。

Bitrix CMS 查询构建器

3.3.1 2023-04-09 00:53 UTC

This package is auto-updated.

Last update: 2023-04-09 00:56:00 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](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();