定制 dmstr/yii2-active-record-search 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dmstr/yii2-active-record-search

Composer 安装命令:

composer require dmstr/yii2-active-record-search

包简介

Highly customizable ActiveRecord search indexer

README 文档

README

This module provides a simple but very flexible way to create a search index of almost any ActiveRecord entries.

The base idea is:

  • we want a search index for every (configured) app language
  • we want to group different Types of Models in search results
  • we want to be able to get simple strings for different Types of Models where we can execute simple LIKE SQL queries on. No matter where the data for these strings come from.
  • we want a fast frontend for the search module, so all required info to build links for the results should be pre-defined

Module Config

see Module for available module params. These should allow to customize almost all aspects of the module and its behavior.

simple example config:

    'modules' => [
        'search' => [
            'class' => \dmstr\activeRecordSearch\Module::class,
            'layout' => '@backend/views/layouts/box',
            'frontendLayout' => '@app/views/layouts/container',
        ],
    ]

The module provides 2 types of controllers:

  • frontend:
    • \dmstr\activeRecordSearch\controllers\FrontendController
  • backend:
    • \dmstr\activeRecordSearch\controllers\SearchGroupController to manage (translate, en/disable) search groups which are created by the indexer
    • \dmstr\activeRecordSearch\controllers\SearchController to manage search items, attentions these will be overwritten by next indexer run. Should usually not be required
    • \dmstr\activeRecordSearch\controllers\SearchGroupTranslationController search group translations. Should usually not be required

Additionally the module provide a simple Search-Input widget:

  • \dmstr\activeRecordSearch\widgets\SearchInput which will be used in frontend controller if not overwritten via modul searchInputWidget property

Indexer

The heart of this module is the SearchIndexer.

The Indexer has to be configured for all Type of Models that should be indexed.

see SearchIndexer for available module params and example.

run the indexer

The Indexer should be defined and called as yii cli cmd:

    $config['controllerMap']['search-index'] = \dmstr\activeRecordSearch\commands\IndexController::class;

To automate indexer runs create cron jobs.

Example script which can run as cron

#!/bin/bash

. /root/export-env
LOG=/tmp/search-index-update
date > $LOG
yii search-index/update >> $LOG
date >> $LOG

Simple Indexer Config example

  • In this example we index 2 types of models (products and accessories)
  • For both types we define the AR model classes. These will be used to get the "data" by calling their find()->all() Methods
  • The string that we will use for the search will be build (concatenation) from the values of the defined attributes
  • For both types we definie the route that should be used to build the URL in results
  • We also define which url_params should be used to build the result URL
  • link_text defines the text for the result Link
 $config['components']['searchIndexer'] = [
     'class' => \dmstr\activeRecordSearch\components\SearchIndexer::class,
     'languages' => function() {
         return project\components\CountryHelper::activeLanguages();
     },
     'fallbackLanguage' => 'en',
     'searchItems' => [
         'products'  => [
             'model_class'      => Product::class,
             'route'      => '/frontend/product/detail',
             'attributes' => [
                 'name',
                 'desc'
             ],
             'url_params' => ['productId' => 'id'],
             'link_text'  => 'name',
             'group'      => 'Products',
         ],
         'accessories'  => [
             'model_class'      => Accessory::class,
             'route'      => '/frontend/accessory/detail',
             'attributes' => [
                 'name',
                 'desc'
             ],
             'url_params' => ['accessoryId' => 'id'],
             'link_text'  => 'name',
             'group'      => 'P&A',
         ],
     ],
 ];
 

Complex Indexer Config example

  • Here we define a bunch of different types where you can see that almost every param can be a callback so that one is able to define "non-static" results.
  • The find_method param can be used to overwrite the default find(). Useful to filter models e.g. by their status flags
  • Callbacks can be used in almost any place e.g. to generate link_text values from more than one attribute or even from attributes of different models (see tags where we use name prefixed by the name from tagGroup relation model)
  • products['attributes'] is an example where you can see how to define virtual attributes from relation models with simple array notation
  • news['attributes']['content'] is an example how to get parts of a json struct as 'content'
  • if you have SEO url rules, you can define all required url_params
  • ...
 $config['components']['searchIndexer'] = [
     'class' => \dmstr\activeRecordSearch\components\SearchIndexer::class,
     'languages' => function() {
         return project\components\CountryHelper::activeLanguages();
     },
     'searchItems' => [
         'products'        => [
             'model_class'      => Product::class,
             'find_method' => function ($item) {
                 return $item['model_class']::find()->andWhere(['archived' => 0]);
             },
             'route'      => '/frontend/product/detail',
             'attributes' => [
                 'name',
                 'frame',
                 'tags' => ['name'],
             ],
             'url_params' => ['productId' => 'id', 'productName' => 'name'],
             'link_text'  => function ($item) {
                 $parts = [];
                 if ($item->getClassificationTag()) {
                     $cTag = $item->getClassificationTag();
                     $cTag !== null && $parts[] = $cTag->name;
                 }
                 $parts[] = $item->name;
                 return implode(': ', array_filter($parts));
             },
             'group'      => 'Products',
         ],
         'product-archive' => [
             'model_class'      => Product::class,
             'find_method' => function ($item) {
                 return $item['model_class']::find()->andWhere(['archived' => 1]);
             },
             'route'      => '/frontend/product/detail',
             'attributes' => [
                 'name',
                 'frame',
                 'tags' => ['name'],
             ],
             'url_params' => ['productId' => 'id', 'productName' => 'name'],
             'link_text'  => function ($item) {
                 $parts = [];
                 if ($item->getClassificationTag()) {
                     $cTag = $item->getClassificationTag();
                     $cTag !== null && $parts[] = $cTag->name;
                 }
                 $parts[] = $item->name;
                 return implode(': ', array_filter($parts));
             },
             'group'      => 'Products Archive',
         ],
         'accessories'  => [
             'model_class'      => Accessory::class,
             'route'      => '/frontend/accessory/detail',
             'attributes' => [
                 'name',
                 'tags' => ['name'],
             ],
             'url_params' => ['accessoryId' => 'id'],
             'link_text'  => 'productName',
             'group'      => 'P&A',
         ],
         'news'         => [
             'model_class'      => PublicationItem::class,
             'route'      => '/publication/default/detail',
             'attributes' => [
                 'title',
                 'content' => function ($item) {
                     $content = Json::decode($item->content_widget_json);
                     return html_entity_decode(strip_tags($content['text_html']));
                 }
             ],
             'url_params' => [
                 'itemId' => 'id',
                 'title'  => function ($item) {
                     return $item->title;
                 }
             ],
             'link_text'  => 'title',
             'group'      => 'News',
         ],
         'tags'         => [
             'model_class'      => \project\modules\cruds\models\Tag::class,
             'route'      => '/productfinder/default/index',
             'find_method' => function ($item) {
                 // get used tagIds from finder
                 $tag_ids = \project\modules\productfinder\models\Productfinder::getFacetIdList('tag_ids');
                 return $item['model_class']::find()->andWhere(['id' => $tag_ids]);
             },
             'attributes' => [
                 'name',
             ],
             'url_params' => [
                 'mainTag'     => 'id',
                 'mainTagName' => 'name',
             ],
             'link_text'  => function ($item) {
                 return implode(': ', [$item->tagGroup->name, $item->name]);
             },
             'group'      => 'Product Tags',
         ],
     ],
 ];

Host info for URL Requests in Console Applications

If there is the need to make HTTP requests to fetch content, a Yii2 console application must be able to generate absolute URLs. Since console apps lack an HTTP context, you need to set the UrlManager's hostInfo property to enable proper URL generation. You can ie. set an ENV CONSOLE_HOST_INFO to the current URL of your page https://example.com/.

'urlManager' => [
    'hostInfo' => getenv('CONSOLE_HOST_INFO'),
]
'pages' => [
    // ...
    'attributes' => [
        'content' => function (Page $item) {
            // This method will fetch data using http requests
            return $item->fetchContentForSearchIndexer();
        }
    ],
    // ...
],

dmstr/yii2-active-record-search 适用场景与选型建议

dmstr/yii2-active-record-search 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.26k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2022 年 02 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「extension」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 dmstr/yii2-active-record-search 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 dmstr/yii2-active-record-search 我们能提供哪些服务?
定制开发 / 二次开发

基于 dmstr/yii2-active-record-search 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 12.26k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 3
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2022-02-16