PHP code example of horat1us / yii2-criteria

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

    

horat1us / yii2-criteria example snippets




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



use Horat1us\Yii\Criteria\PaginationCriteria;

Yii::$container->set(
    PaginationCriteria::class,
    [
        'class' => PaginationCriteria::class,
        
        'page' => 1,
        'pageSize' => 10,
        
        'allowedPageSize' => [1, 5, 10,],
    ]
);