/
v.bolshakov
/
AIEcosystem-Testing
Обзор
Документация
Войти
/
v.bolshakov
/
AIEcosystem-Testing
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
common/filter/base/BaseFilter.php
143 строки
3 KB
Developer
Initial commit
03 авг 2026, 17:43
03 авг 2026, 17:43
7433917
Код
Авторство
О чём код?
<?php namespace common\filter\base; use common\search\base\BaseSearch; use Yii; use yii\base\InvalidConfigException; use yii\base\Model; use yii\data\DataFilter; use yii\helpers\VarDumper; /** * Базовый класс для фильтрации в поиске моделей * * @author Dmitry E. Semenov <sde.tomsk@gmail.com> * @copyright Self (c) 2019 */ abstract class BaseFilter extends Model { /** * Имя класса фильтра * @var string */ protected $dataFilter = 'yii\data\ActiveDataFilter'; /** * Модель для поиска * @var BaseSearch */ protected $searchModel = null; /** * Данные для фильтрации * @var null */ protected $filter = null; /** * @inheritdoc */ public function init() { parent::init(); } /** * @return array */ protected function attributeMap() { return []; } /** * {@inheritdoc} */ protected function doSearch() { if ($this->filter) { $this->searchModel->query ->andWhere($this->filter); return true; } else { return false; } } /** * Вызывает событие до выборки по фильтруемым полям * @return mixed */ public function beforeSearch() { return true; } /** * Вызывает событие после выборки по фильтруемым полям */ public function afterSearch() { } /** * Установить поисковую модуль для фильтра * @param BaseSearch $model */ public function setSearchModel(BaseSearch $model) { $this->searchModel = $model; } /** * @param array $data * @param null $formName * @return bool|void * @throws InvalidConfigException */ public function load($data, $formName = null) { parent::load($data, $formName); /** @var DataFilter $dataFilter */ $dataFilter = Yii::createObject($this->dataFilter); $dataFilter->searchModel = $this; $dataFilter->attributeMap = $this->attributeMap(); if ($dataFilter->load($data)) { if (!$this->filter = $dataFilter->build()) { $this->addErrors($dataFilter->errors); Yii::debug(VarDumper::dumpAsString([ 'errors' => $dataFilter->errors ])); } } } /** * Выполнить поиск по данным * * @return mixed */ public function execute() { if (!$this->validate()) { Yii::debug(VarDumper::dumpAsString([ 'errors' => $this->errors ])); return false; } $result = true; if ($this->beforeSearch()) { if ($result = $this->doSearch()) { $this->afterSearch(); } } return $result; } }