PHP code example of vrtc / yii2-querybuilder

1. Go to this page and download the library: Download vrtc/yii2-querybuilder 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/ */

    

vrtc / yii2-querybuilder example snippets



use vrtc\querybuilder\QueryBuilderForm;

 QueryBuilderForm::begin([
    'rules' => $rules,
    'builder' => [
        'id' => 'query-builder',
        'filters' => [
            ['id' => 'id', 'label' => 'Id', 'type' => 'integer'],
            ['id' => 'name', 'label' => 'Name', 'type' => 'string'],
            ['id' => 'lastName', 'label' => 'Last Name', 'type' => 'string']
        ]
    ]
 ])


use vrtc\querybuilder\Translator;

public function actionIndex()
{
      $query = Customer::find();
      $rules = Json::decode(Yii::$app->request->get('rules'));
      if ($rules) {
          $translator = new Translator($rules);
          $query
            ->andWhere($translator->where())
            ->addParams($translator->params());
      }
      
      $dataProvider = new ActiveDataProvider([
          'query' => $query,
      ]);
    
      return $this->render('index', [
          'dataProvider' => $dataProvider,
          'rules' => $rules
      ]);
}

        if (!is_null($rules) && !empty($rules['rules'])) {
            if ($rules) {
                $translator = new Translator($rules);
                $query->andWhere($translator->where())
                    ->addParams($translator->params());
            }
        }
        

           public function getFilters()
    {

        $return[] = [
            'id' => 'quantity',
            'label' => 'Количество',
            'operators' => ['less', 'less_or_equal', 'equal', 'greater', 'greater_or_equal'],
            'type' => 'integer',
        ];
        $return[] = [
            'id' => 'stocks',
            'label' => 'Наличие на складах',
            'type' => 'string',
            'input' => 'select',
            'operators' => ['in', 'not_in'],
            'values' => WareHouses::getWarehousesList(),
        ];
        $return[] = [
            'id' => 'name',
            'label' => 'Название',
            'type' => 'string',
            'operators' => ['contains', 'equal'],
        ];
        $return[] = [
            'id' => 'category_id',
            'label' => 'Категории',
            'type' => 'string',
            'input' => 'select',
            'multiple' => true,
            /* 'values' => $this->categoriesList(),*/
            'plugin' => 'select2',
            'pluginConfig' => [
                'data' => $this->categoriesListForAdminFilter(),
                'multiple' => true,
                'width' => '100%'
            ],
            'operators' => ['in', 'not_in', 'is_null'],
        ];
        return $return;
    }
    

php composer.phar