brezgalov / yii2-filters
yii2
v1.0.3
2019-04-25 12:59 UTC
Requires
- php: >=5.4.0
- yiisoft/yii2: ~2.0.14
This package is auto-updated.
Last update: 2024-09-26 00:29:52 UTC
README
从 brezgalov\filters\actions\IndexAction 继承 IndexAction。现在除了 conditions、'expand'、'per-page'、'page' 之外的所有 query-参数都将用作过滤条件。例如
users?id=1 - ищем пользователя с id = 1 users?id[]=23&id[]=42&conditions[id]="in" - ищем пользователя с id 23 или 42
自定义过滤
创建过滤模型
class InnFilter extends BaseObject implements IFilterHandler { public function apply(\yii\db\QueryInterface $query) { $inn = \Yii::$app->request->getQueryParam('inn'); $phone = \Yii::$app->request->getQueryParam('phone'); if (!$inn && $phone) { throw new UnprocessableEntityHttpException('Фильтр по инн не может функционировать без параметров inn и phone'); } $query ->join('inner join', 'client_requisites as inn_client_reqs', 'drivers.client_id = inn_client_reqs.client_id') ->andWhere(['inn_client_reqs.inn' => $inn]) ->andWhere([ 'or', ['drivers.phone1' => $phone], ['drivers.phone2' => $phone] ]) ; } }
然后,在 IndexAction 中连接过滤
class IndexAction extends \brezgalov\filters\actions\IndexAction { public function __construct($id, \yii\base\Controller $controller, array $config = []) { parent::__construct($id, $controller, $config); $this->filter->addHandler('inn', new InnFilter()); } }
使用方法
/drivers?filter=inn&inn=12345678&phone=79091234555