2017 © Pedro Peláez
 

yii2-extension yii2-refiner

Helps to add additional where params to active query (refine query)

image

pahanini/yii2-refiner

Helps to add additional where params to active query (refine query)

  • Wednesday, September 17, 2014
  • by pahanini
  • Repository
  • 3 Watchers
  • 5 Stars
  • 427 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 5 Forks
  • 1 Open issues
  • 7 Versions
  • 1 % Grown

The README.md

Yii2 search refiner (facet search)

Helps to add additional 'where' conditions based on http request to active queries (refine search queries) and also automates calculation of possible values of different filters used to build UI. Can be used in RESTful APIs., (*1)

Terms

"Base query" - ActiveQuery (e.g. MySqlQuery, SphinxQuery) to find all possible items before any refiner will be applied. E.g. SELECT * FROM products WHERE balance > 0, (*2)

"Refiner" is object which: - calculates all possible (or independent) values for UI, e.g. list of possible product's categories, maximum and minimum price between products (all callback). - analyzes http request and add additional where conditions to basic query (refine callback) - calculates active values of UI. Active values depends on values of other refiners (active callback)., (*3)

"Refiner Set" - object which contain one or more refiners and base query. Refiner set applies all refiners to query., (*4)

Installation

Add, (*5)

"pahanini/yii2-refiner": "*", (*6)

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

Usage

Set

Refiner set has two main independent functions:, (*8)

  • getRefinedQuery - returns query modified by refiners
  • getRefinerValues - returns all refiners values

Refiner

Each refiner modifies base query according to http query params. To modify query refiner calls refine($query, $params) callback function. This function must return modified query. Function example:, (*9)

$this->refine = function($query, $params) {
    return $query->andWhere('has_discount > :val', [':val' => $params]);
}

Note: - if http param does no exist refine function will not be called - if $paramSeparator property is set and http param is not an array then http param wll converted to array using $paramSeparator property - if $paramToArray property is set and http param is not an array then it will converted to array with one element - if $paramType is set then http param type will be changed using php setType function, (*10)

Some examples:, (*11)

public function init()
{
    $this->refinerSet = new \pahanini\refiner\Set([
        'refiners' => [
            // standard range
            'price' => [
                'class' => '\pahanini\refiner\db\Range',
            ],
            'category' => [
                'class' => '\pahanini\refiner\db\Count',
            ],
            // Select color values from another model
            'onlyRedColor' => [
                'refine' => function($query, $param) {
                    return $query->andWhere('color = "red"');
                },
                'all' => function($query) {
                    return $query->select('COUNT(*)')->andWhere('color = "red"')
                }
            ]
        ]
    ])
}


public function actionSearch()
{
    $query = Product::find()->andWhere('balance > 0');
    $refinerResult = $this->refinerSet->applyTo($query);
    $this->render('search', ['query' => $query, 'refiners' => $this->refinerSet->getRefinerValues()])
}

The Versions

17/09 2014

dev-master

9999999-dev https://github.com/pahanini/yii2-refiner

Helps to add additional where params to active query (refine query)

  Sources   Download

BSD-3-Clause

by Pavel Tetyaev

yii2 facet search refine search

09/07 2014

v0.6

0.6.0.0 https://github.com/pahanini/yii2-refiner

Helps to add additional where params to active query (refine query)

  Sources   Download

BSD-3-Clause

by Pavel Tetyaev

yii2 facet search refine search

09/07 2014

v0.5

0.5.0.0 https://github.com/pahanini/yii2-refiner

Helps to add additional where params to active query (refine query)

  Sources   Download

BSD-3-Clause

by Pavel Tetyaev

yii2 facet search refine search

17/06 2014

v0.4

0.4.0.0 https://github.com/pahanini/yii2-refiner

Helps to add additional where params to active query (refine query)

  Sources   Download

BSD-3-Clause

by Pavel Tetyaev

yii2 facet search refine search

16/06 2014

v0.3

0.3.0.0 https://github.com/pahanini/yii2-refiner

Helps to add additional where params to active query (refine query)

  Sources   Download

BSD-3-Clause

by Pavel Tetyaev

yii2 facet search refine search

19/05 2014

v0.2

0.2.0.0 https://github.com/pahanini/yii2-refiner

Helps to add additional where params to query (refine query)

  Sources   Download

BSD-3-Clause

by Pavel Tetyaev

yii2 facet search refine search

15/05 2014

v0.1

0.1.0.0 https://github.com/pahanini/yii2-refiner

Helps to add additional where params to query (refine query)

  Sources   Download

BSD-3-Clause

by Pavel Tetyaev

yii2 facet search refine search