horat1us / yii2-criteria
Criteria implementation for Yii2
Installs: 1 653
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 2
Requires
- php: >=7.0
- yiisoft/yii2: ^2.0.13.1
Requires (Dev)
- phpunit/phpunit: ^6.5
README
This package helps dealing with multiple search params.
Installing
Using composer:
composer require horat1us/yii2-criteria
Requires PHP >= 7.0, yii2 >= 2.0.13
Usage
This code will find SomeActiveRecord
with specified id (from URL).
<?php use YourApp\SomeActiveRecord; use yii\db\Query; use yii\base\Model; use Horat1us\Yii\Criteria\Interfaces\CriteriaInterface; use Horat1us\Yii\Criteria\Factories\QueryFactory; $query = SomeActiveRecord::find(); $factory = new QueryFactory($query); $factory->push(new class extends Model implements CriteriaInterface { public $id; public function rules() { return [['id', 'integer',]]; } public function formName() { return 'IdCriteria'; } public function apply(Query $query): Query { return $query->andWhere(['=', 'id', (int)$this->id]); } }); $record = $factory->apply(\Yii::$app->request->queryParams)->one(); print_r($record->id, $_GET['IdCriteria']['id']); // true
Pre-cooked criteria
Factories
Tips
To set default params for criteria (for example, PaginationCriteria) configure your yii\db\Container
in bootstrap.php
file:
<?php use Horat1us\Yii\Criteria\PaginationCriteria; Yii::$container->set( PaginationCriteria::class, [ 'class' => PaginationCriteria::class, 'page' => 1, 'pageSize' => 10, 'allowedPageSize' => [1, 5, 10,], ] );
Contributors
License
Todo
- Tests