2017 © Pedro Peláez
 

yii2-extension yii2-data-structure-tools

Data structure collections, helpers and utilities for yii2 ActiveRecord models(trees, sorts and etc.)

image

devgroup/yii2-data-structure-tools

Data structure collections, helpers and utilities for yii2 ActiveRecord models(trees, sorts and etc.)

  • Wednesday, May 10, 2017
  • by bethrezen
  • Repository
  • 8 Watchers
  • 8 Stars
  • 992 Installations
  • PHP
  • 10 Dependents
  • 0 Suggesters
  • 4 Forks
  • 5 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Yii2 data structures tools

TBD, (*1)

WARNING: This extension is under active development. Don't use it in production!, (*2)

Build Status codecov.io, (*3)

Installation

The preferred way to install this extension is through composer., (*4)

Either run, (*5)

php composer.phar require --prefer-dist devgroup/yii2-data-structure-tools "*"

or add, (*6)

"devgroup/yii2-data-structure-tools": "*"

to the require section of your composer.json file., (*7)

Usage

TBD, (*8)

Credits and inspiration sources

Search concept

for now it works only with STATIC VALUES property storage, (*9)

Note, that search will work only with models with DevGroup\DataStructure\traits\PropertiesTrait trait and DevGroup\DataStructure\behaviors\HasProperties behavior connected. See how to connect (Russian for now), (*10)

Extension provides flexible system of search. Each property have configuration point that switches ability to use this property in search., (*11)

Basic search will be done in two ways:, (*12)

  • common search against regular databases e.g.: mysql, mariadb etc;
  • elasticsearch indices search.

Main feature is that if you want to use elasticseatch, and defined it in the app config, but not still configure it your search will just work fine with auto fallback to simple mysql searching. And when your elasticsearch will be properly started search will be automatically switched for elasticseatch., (*13)

Preferred search mechanism you can define in the application configuration files, like this:, (*14)

    'modules' => [
    ...
         'properties' => [
             'class' => 'DevGroup\DataStructure\Properties\Module',
             'searchClass' => \DevGroup\DataStructure\searchOld\elastic\Search::class,
             'searchConfig' => [
                 'hosts' => ['host1:9200', 'https://host2:9200'],
                 'watcherClass' => MyWatch::class,
             ]

         ],
    ...
    ],
  • searchClass - class to be used for search. If omit - there will be no search configured,
  • searchConfig - array additional parameters to be applied for search object, except common search. For elastic search following special keys may be set:
    • hosts see hosts config,
    • watcherClass - you can use your own watcher for elsticsearch if needed.

If you want to start using elasticsearch, first of all you have to install and configure it., (*15)

Then, if you already have entries in your database you may want to generate and load start indices. For this run in console:, (*16)

./yii properties/elastic/fill-index

This command will create indices for all properties that you allowed to search., (*17)

How to search

For now only available to perform filtering against properties static values, (*18)

At any place you want

<?= \DevGroup\DataStructure\search\widgets\FilterFormWidget::widget([
    'modelClass' => \app\models\Page::class,
    'filterRoute' => ['/url/to/filter'],
    'config' => [
        'storage' => [
            EAV::class,
            StaticValues::class,
        ]
    ]
]) ?>

This will render basic filter form with all properties and values contained in the elasticsearch index - 'modelClass' - required param, any model class name you have in your app with assigned properties and their static values, - 'filterRoute' - required param, action attribute for rendered filter form, - 'config' - optional, additional array of config. Special key storage will be used for definition against what property storage search will be proceed. If you omit it search will be work only against StaticValues storage by default, (*19)

In your controller

public function actionFilter()
{
  /** @var AbstractSearch $component */
  $search = \Yii::$app->getModule('properties')->getSearch();
  $config = ['storage' => [
      EAV::class,
      StaticValues::class,
    ]
  ];
  $modelIds = $search->filterByProperties(Page::class, $config, \Yii::$app->request->get('filter'));
  $dataProvider = new ActiveDataProvider([
      //provider config
  ]);
  //other stuff here
}
  • Page - any model class name you have in your app with assigned properties and their static values
  • $modelIds will contain all found model ids, according to selected property values in filter. Using them you can show anything you want,
  • '$config' - optional, additional array of config. Special key storage will be used for definition against what property storage search will be proceed. If you omit it search will be work only against StaticValues storage by default

Filtering logic

Filters uses both intersection and union operations while search., (*20)

Lets see, for example you have filter request like this:, (*21)

[
    1 => [2,3],
    13 => [18,9,34]
]

First of all this means that we want to find products that has property values assigned with id 2,3 from property with id 1, and 18, 9, 34 from property with id 13., (*22)

What will filter do? - For now it will find all products with assigned values with ids IN(2,3); - then it will find all products with assigned values with ids IN(12,9,34); - and finally it will return to you result of intersection from both previous results., (*23)

How to extend and implement

For all Search and Watch mechanisms you can use your custom implementation., (*24)

Actually you can create and use your own database connection, e.g.: MongoDB, ArangoDB., (*25)

Or you can just use your custom Watch class for elasticsearch index actualization., (*26)

Both Search and Watch classes are implements according interfaces and extends abstract classes - DevGroup\DataStructure\search\interfaces\Search and DevGroup\DataStructure\search\base\AbstractSearch for Search - DevGroup\DataStructure\search\interfaces\Watch and DevGroup\DataStructure\search\base\AbstractWatch for Watch, (*27)

Just extend your class from needed abstract class and define it in application config, like described upper., (*28)

If you are realizing custom index, you probably need to create own controller for first time index initialization, like DevGroup\DataStructure\commands\ElasticIndexController, (*29)

Define your own Watch class in your own Search class if necessary., (*30)

Clearly created and defined Watch class will be automatically subscribed to according system events., (*31)

The Versions