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