PHP code example of dmstr / yii2-active-record-search

1. Go to this page and download the library: Download dmstr/yii2-active-record-search library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dmstr / yii2-active-record-search example snippets


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

 $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',
         ],
     ],
 ];
 

 $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',
         ],
     ],
 ];