yii2tech/ar-search
最新稳定版本:1.0.1
Composer 安装命令:
composer require yii2tech/ar-search
包简介
Provides unified search model for Yii ActiveRecord
README 文档
README
ActiveRecord Search Model Extension for Yii2
This extension provides unified search model for Yii ActiveRecord.
For license information check the LICENSE-file.
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist yii2tech/ar-search
or add
"yii2tech/ar-search": "*"
to the require section of your composer.json.
Usage
This extension provides unified search model for Yii ActiveRecord via special model class -
\yii2tech\ar\search\ActiveSearchModel.
This model is able to fetch its attributes, validation rules and filtering logic from the 'slave'
source ActiveRecord model specified via \yii2tech\ar\search\ActiveSearchModel::$model.
Thus you do not need to declare a separated model class for searching and define a filter logic.
For example:
<?php use yii2tech\ar\search\ActiveSearchModel; $searchModel = new ActiveSearchModel([ 'model' => 'app\models\Item' ]); $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
ActiveSearchModel picks all 'safe' attributes of the 'slave' model and use them as own attributes.
Thus you can use any attribute, which is marked as 'safe' in related ActiveRecord model in the scope
of this class. For example:
<?php namespace app\models; // ActiveRecord to be searched: class Item extends \yii\db\ActiveRecord { public function rules() { return [ [['name', 'status', 'price'], 'required'], ['name', 'string'], ['status', 'integer'], ['price', 'number'], ]; } } use yii2tech\ar\search\ActiveSearchModel; // Create search model for declared ActiveRecord: $searchModel = new ActiveSearchModel([ 'model' => 'app\models\Item' ]); // safe attributes of `Item` are inherited: $searchModel->name = 'Paul'; $searchModel->price = 10.5;
Inherited attributes may be used while composing web forms, which should collect filter data. For example:
<?php use yii2tech\ar\search\ActiveSearchModel; use yii\widgets\ActiveForm; $searchModel = new ActiveSearchModel([ 'model' => 'app\models\Item' ]); ?> <?php $form = ActiveForm::begin(); ?> <?= $form->field($model, 'name')->textInput() ?> <?= $form->field($model, 'price')->textInput() ?> ... <?php ActiveForm::end(); ?>
Attribute labels and hints are also inherited from the 'slave' model.
The main method of \yii2tech\ar\search\ActiveSearchModel is search(). It loads filter attributes
from given data array, validates them an creates a \yii\data\ActiveDataProvider instance applying
own attributes as a query filter condition.
ActiveSearchModel uses a sophisticated logic for the query filtering, based on the attribute types,
specified by \yii2tech\ar\search\ActiveSearchModel::$searchAttributeTypes, which value is extracted
from \yii2tech\ar\search\ActiveSearchModel::$model by default and filter operators list, specified via
\yii2tech\ar\search\ActiveSearchModel::$filterOperators.
By default \yii\db\QueryInterface::andFilterWhere() will be used for the filter composition. For the
'string' attributes it will be used with 'like' operator. For 'integer' and 'float' ('double') method
andFilterCompare() will be used, if it is available.
Heads up! Do not abuse ActiveSearchModel usage. It has been designed to cover only the simplest
cases, when search logic is trivial. You should always create a separated search model in case, it
requires complex logic of composition of the search query.
Adjusting Data Provider
You may want to change some settings of the data provider, created by the search() method: change
pagination or sort settings and so on. You can do this via \yii2tech\ar\search\ActiveSearchModel::$dataProvider.
For example:
<?php use yii2tech\ar\search\ActiveSearchModel; $searchModel = new ActiveSearchModel([ 'model' => 'app\models\Item', 'dataProvider' => [ 'class' => 'yii\data\ActiveDataProvider', 'pagination' => [ 'defaultPageSize' => 40 ], ], ]); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); echo $dataProvider->pagination->defaultPageSize; // outputs `40`
Adjusting Search Query
You may use \yii2tech\ar\search\ActiveSearchModel::EVENT_AFTER_CREATE_QUERY event to adjust the search query instance
adding relation eager loading or permanent conditions. For example:
<?php use yii2tech\ar\search\ActiveSearchModel; use yii2tech\ar\search\ActiveSearchEvent; $searchModel = new ActiveSearchModel([ 'model' => 'app\models\Item', ]); $searchModel->on(ActiveSearchModel::EVENT_AFTER_CREATE_QUERY, function(ActiveSearchEvent $event) { $event->query ->with(['category']) ->andWhere(['status' => 1]); });
You may also specify query object directly via \yii2tech\ar\search\ActiveSearchModel::$dataProvider. For example:
<?php use yii2tech\ar\search\ActiveSearchModel; use yii\data\ActiveDataProvider; use app\models\Item; $searchModel = new ActiveSearchModel([ 'model' => Item::className(), 'dataProvider' => function () { $query = Item::find() ->with(['category']) ->andWhere(['status' => 1]); return ActiveDataProvider(['query' => $query]); }, ]);
Filter Operators
You can control the operators to be used for the query filtering via \yii2tech\ar\search\ActiveSearchModel::$filterOperators.
It defines a mapping between the attribute type and the operator to be used with \yii\db\QueryInterface::andFilterWhere().
Each value can be a scalar operator name or a PHP callback, accepting query instance, attribute name and value.
For example:
<?php use yii2tech\ar\search\ActiveSearchModel; $searchModel = new ActiveSearchModel([ 'model' => 'app\models\Item', 'filterOperators' => [ ActiveSearchModel::TYPE_STRING => '=', // use strict comparison for the string attributes ActiveSearchModel::TYPE_INTEGER => function (\yii\db\ActiveQueryInterface $query, $attribute, $value) { if ($attribute === 'commentsCount') { $query->andHaving(['commentsCount' => $value]); } else { $query->andFilterWhere([$attribute => $value]); } }, ], ]);
ActiveSearchModel allows filtering for the attributes using andFilterCompare() method of the query (for example:
\yii\db\Query::andFilterCompare()), which allows specifying filter value in format: {operator}{value} (for
example: >10, <=100 and so on). The list of attribute names, for which usage of such comparison is allowed is controlled
by \yii2tech\ar\search\ActiveSearchModel::$compareAllowedAttributes. For example:
<?php use yii2tech\ar\search\ActiveSearchModel; $searchModel = new ActiveSearchModel([ 'model' => 'app\models\Item', 'compareAllowedAttributes' => [ 'price' // allow compare for 'price' only, excluding such fields like 'categoryId', 'status' and so on. ], ]);
You can set compareAllowedAttributes to *, which indicates any float or integer attribute will be allowed for comparison.
Note:
\yii2tech\ar\search\ActiveSearchModel::$filterOperatorstake precedence over\yii2tech\ar\search\ActiveSearchModel::$compareAllowedAttributes.
Working Without 'Slave' Model
Although in most cases setup of \yii2tech\ar\search\ActiveSearchModel::$model is a quickest way to configure ActiveSearchModel
instance, it is not mandatory. You can avoid setup of the 'slave' model and configure all search related properties
directly. For example:
<?php use yii2tech\ar\search\ActiveSearchModel; use yii\data\ActiveDataProvider; use app\models\Item; $searchModel = new ActiveSearchModel([ 'searchAttributeTypes' => [ 'id' => ActiveSearchModel::TYPE_INTEGER, 'name' => ActiveSearchModel::TYPE_STRING, 'price' => ActiveSearchModel::TYPE_FLOAT, ], 'rules' => [ ['id', 'integer'], ['name', 'string'], ['price', 'number'], ], 'compareAllowedAttributes' => [], 'dataProvider' => function () { $query = Item::find() ->with(['category']) ->andWhere(['status' => 1]); return ActiveDataProvider(['query' => $query]); }, ]);
yii2tech/ar-search 适用场景与选型建议
yii2tech/ar-search 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.61k 次下载、GitHub Stars 达 30, 最近一次更新时间为 2016 年 10 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「search」 「filter」 「active」 「record」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yii2tech/ar-search 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yii2tech/ar-search 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yii2tech/ar-search 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel package to retrieve data from Google Search Console
Propel2 is an open-source Object-Relational Mapping (ORM) for PHP 5.5 and up.
Nova searchable filter for belongsTo relationships.
A Symfony extension to get active class base on current bundle/controller/action
Indexed Search Autocomplete - Extends the TYPO3 Core Extension Indexed_Search searchform with an autocomplete feature.
LDAP user provider bundle for Symfony 6.4
统计信息
- 总下载量: 8.61k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 32
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2016-10-13