{"id":28635425,"url":"https://github.com/dmstr/yii2-active-record-search","last_synced_at":"2026-02-16T23:07:49.219Z","repository":{"id":56970289,"uuid":"459928460","full_name":"dmstr/yii2-active-record-search","owner":"dmstr","description":"Highly customizable ActiveRecord search indexer for yii2","archived":false,"fork":false,"pushed_at":"2024-05-08T07:18:30.000Z","size":50,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-06-12T17:11:23.168Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dmstr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-02-16T09:09:28.000Z","updated_at":"2024-05-08T07:17:46.000Z","dependencies_parsed_at":"2025-06-12T17:10:21.346Z","dependency_job_id":"ca32f119-955c-47f4-9961-29029df05e6f","html_url":"https://github.com/dmstr/yii2-active-record-search","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/dmstr/yii2-active-record-search","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmstr%2Fyii2-active-record-search","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmstr%2Fyii2-active-record-search/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmstr%2Fyii2-active-record-search/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmstr%2Fyii2-active-record-search/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dmstr","download_url":"https://codeload.github.com/dmstr/yii2-active-record-search/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmstr%2Fyii2-active-record-search/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260838649,"owners_count":23070610,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-06-12T17:10:14.274Z","updated_at":"2026-02-16T23:07:49.212Z","avatar_url":"https://github.com/dmstr.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# yii2-active-record-search\n\nThis module provides a simple but very flexible way to create a search index of almost any ActiveRecord entries.\n\nThe base idea is:\n\n- we want a search index for every (configured) app language\n- we want to group different Types of Models in search results\n- 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.\n- we want a fast frontend for the search module, so all required info to build links for the results should be pre-defined\n\n## Module Config\n\nsee [Module](src/Module.php) for available module params.\nThese should allow to customize almost all aspects of the module and its behavior.\n\nsimple example config:\n\n```php\n    'modules' =\u003e [\n        'search' =\u003e [\n            'class' =\u003e \\dmstr\\activeRecordSearch\\Module::class,\n            'layout' =\u003e '@backend/views/layouts/box',\n            'frontendLayout' =\u003e '@app/views/layouts/container',\n        ],\n    ]\n```\n\nThe module provides 2 types of controllers:\n\n- frontend:\n    - `\\dmstr\\activeRecordSearch\\controllers\\FrontendController`\n- backend:\n    - `\\dmstr\\activeRecordSearch\\controllers\\SearchGroupController` to manage (translate, en/disable) search groups which are created by the indexer\n    - `\\dmstr\\activeRecordSearch\\controllers\\SearchController` to manage search items, attentions these will be overwritten by next indexer run. Should usually not be required\n    - `\\dmstr\\activeRecordSearch\\controllers\\SearchGroupTranslationController` search group translations. Should usually not be required\n\nAdditionally the module provide a simple Search-Input widget:\n\n- `\\dmstr\\activeRecordSearch\\widgets\\SearchInput`\n  which will be used in frontend controller if not overwritten via modul `searchInputWidget` property\n\n## Indexer\n\nThe heart of this module is the [SearchIndexer](src/components/SearchIndexer.php).\n\nThe Indexer has to be configured for all Type of Models that should be indexed.\n\nsee [SearchIndexer](src/components/SearchIndexer.php) for available module params and example.\n\n### run the indexer\n\nThe Indexer should be defined and called as yii cli cmd:\n\n```\n    $config['controllerMap']['search-index'] = \\dmstr\\activeRecordSearch\\commands\\IndexController::class;\n```\n\nTo automate indexer runs create cron jobs.\n\nExample script which can run as cron\n\n```\n#!/bin/bash\n\n. /root/export-env\nLOG=/tmp/search-index-update\ndate \u003e $LOG\nyii search-index/update \u003e\u003e $LOG\ndate \u003e\u003e $LOG\n```\n\n### Simple Indexer Config example\n\n- In this example we index 2 types of models (products and accessories)\n- For both types we define the AR model classes. These will be used to get the \"data\" by calling their `find()-\u003eall()` Methods\n- The string that we will use for the search will be build (concatenation) from the values of the defined `attributes`\n- For both types we definie the route that should be used to build the URL in results\n- We also define which url_params should be used to build the result URL\n- `link_text` defines the text for the result Link\n\n```php\n $config['components']['searchIndexer'] = [\n     'class' =\u003e \\dmstr\\activeRecordSearch\\components\\SearchIndexer::class,\n     'languages' =\u003e function() {\n         return project\\components\\CountryHelper::activeLanguages();\n     },\n     'fallbackLanguage' =\u003e 'en',\n     'searchItems' =\u003e [\n         'products'  =\u003e [\n             'model_class'      =\u003e Product::class,\n             'route'      =\u003e '/frontend/product/detail',\n             'attributes' =\u003e [\n                 'name',\n                 'desc'\n             ],\n             'url_params' =\u003e ['productId' =\u003e 'id'],\n             'link_text'  =\u003e 'name',\n             'group'      =\u003e 'Products',\n         ],\n         'accessories'  =\u003e [\n             'model_class'      =\u003e Accessory::class,\n             'route'      =\u003e '/frontend/accessory/detail',\n             'attributes' =\u003e [\n                 'name',\n                 'desc'\n             ],\n             'url_params' =\u003e ['accessoryId' =\u003e 'id'],\n             'link_text'  =\u003e 'name',\n             'group'      =\u003e 'P\u0026A',\n         ],\n     ],\n ];\n \n```\n\n### Complex Indexer Config example\n\n- 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.\n- The find_method param can be used to overwrite the default find(). Useful to filter models e.g. by their status flags\n- 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\n  prefixed by the name from tagGroup relation model)\n- `products['attributes']` is an example where you can see how to define virtual attributes from relation models with simple array notation\n- `news['attributes']['content']` is an example how to get parts of a json struct as 'content'\n- if you have SEO url rules, you can define all required `url_params`\n- ...\n\n```php\n $config['components']['searchIndexer'] = [\n     'class' =\u003e \\dmstr\\activeRecordSearch\\components\\SearchIndexer::class,\n     'languages' =\u003e function() {\n         return project\\components\\CountryHelper::activeLanguages();\n     },\n     'searchItems' =\u003e [\n         'products'        =\u003e [\n             'model_class'      =\u003e Product::class,\n             'find_method' =\u003e function ($item) {\n                 return $item['model_class']::find()-\u003eandWhere(['archived' =\u003e 0]);\n             },\n             'route'      =\u003e '/frontend/product/detail',\n             'attributes' =\u003e [\n                 'name',\n                 'frame',\n                 'tags' =\u003e ['name'],\n             ],\n             'url_params' =\u003e ['productId' =\u003e 'id', 'productName' =\u003e 'name'],\n             'link_text'  =\u003e function ($item) {\n                 $parts = [];\n                 if ($item-\u003egetClassificationTag()) {\n                     $cTag = $item-\u003egetClassificationTag();\n                     $cTag !== null \u0026\u0026 $parts[] = $cTag-\u003ename;\n                 }\n                 $parts[] = $item-\u003ename;\n                 return implode(': ', array_filter($parts));\n             },\n             'group'      =\u003e 'Products',\n         ],\n         'product-archive' =\u003e [\n             'model_class'      =\u003e Product::class,\n             'find_method' =\u003e function ($item) {\n                 return $item['model_class']::find()-\u003eandWhere(['archived' =\u003e 1]);\n             },\n             'route'      =\u003e '/frontend/product/detail',\n             'attributes' =\u003e [\n                 'name',\n                 'frame',\n                 'tags' =\u003e ['name'],\n             ],\n             'url_params' =\u003e ['productId' =\u003e 'id', 'productName' =\u003e 'name'],\n             'link_text'  =\u003e function ($item) {\n                 $parts = [];\n                 if ($item-\u003egetClassificationTag()) {\n                     $cTag = $item-\u003egetClassificationTag();\n                     $cTag !== null \u0026\u0026 $parts[] = $cTag-\u003ename;\n                 }\n                 $parts[] = $item-\u003ename;\n                 return implode(': ', array_filter($parts));\n             },\n             'group'      =\u003e 'Products Archive',\n         ],\n         'accessories'  =\u003e [\n             'model_class'      =\u003e Accessory::class,\n             'route'      =\u003e '/frontend/accessory/detail',\n             'attributes' =\u003e [\n                 'name',\n                 'tags' =\u003e ['name'],\n             ],\n             'url_params' =\u003e ['accessoryId' =\u003e 'id'],\n             'link_text'  =\u003e 'productName',\n             'group'      =\u003e 'P\u0026A',\n         ],\n         'news'         =\u003e [\n             'model_class'      =\u003e PublicationItem::class,\n             'route'      =\u003e '/publication/default/detail',\n             'attributes' =\u003e [\n                 'title',\n                 'content' =\u003e function ($item) {\n                     $content = Json::decode($item-\u003econtent_widget_json);\n                     return html_entity_decode(strip_tags($content['text_html']));\n                 }\n             ],\n             'url_params' =\u003e [\n                 'itemId' =\u003e 'id',\n                 'title'  =\u003e function ($item) {\n                     return $item-\u003etitle;\n                 }\n             ],\n             'link_text'  =\u003e 'title',\n             'group'      =\u003e 'News',\n         ],\n         'tags'         =\u003e [\n             'model_class'      =\u003e \\project\\modules\\cruds\\models\\Tag::class,\n             'route'      =\u003e '/productfinder/default/index',\n             'find_method' =\u003e function ($item) {\n                 // get used tagIds from finder\n                 $tag_ids = \\project\\modules\\productfinder\\models\\Productfinder::getFacetIdList('tag_ids');\n                 return $item['model_class']::find()-\u003eandWhere(['id' =\u003e $tag_ids]);\n             },\n             'attributes' =\u003e [\n                 'name',\n             ],\n             'url_params' =\u003e [\n                 'mainTag'     =\u003e 'id',\n                 'mainTagName' =\u003e 'name',\n             ],\n             'link_text'  =\u003e function ($item) {\n                 return implode(': ', [$item-\u003etagGroup-\u003ename, $item-\u003ename]);\n             },\n             'group'      =\u003e 'Product Tags',\n         ],\n     ],\n ];\n```\n\n## Host info for URL Requests in Console Applications\n\nIf there is the need to make HTTP requests to fetch content, a Yii2 console application must be able to generate absolute URLs.\nSince console apps lack an HTTP context, you need to set the UrlManager's hostInfo property to enable proper URL generation.\nYou can ie. set an ENV `CONSOLE_HOST_INFO` to the current URL of your page `https://example.com/`.\n\n```php\n'urlManager' =\u003e [\n    'hostInfo' =\u003e getenv('CONSOLE_HOST_INFO'),\n]\n```\n\n```php\n'pages' =\u003e [\n    // ...\n    'attributes' =\u003e [\n        'content' =\u003e function (Page $item) {\n            // This method will fetch data using http requests\n            return $item-\u003efetchContentForSearchIndexer();\n        }\n    ],\n    // ...\n],\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdmstr%2Fyii2-active-record-search","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdmstr%2Fyii2-active-record-search","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdmstr%2Fyii2-active-record-search/lists"}