yii2mod / yii2-array-query
Yii2 组件,允许对数组元素进行搜索/筛选。
1.4
2019-08-26 20:03 UTC
Requires
- php: >=7.0.0
- yiisoft/yii2: *
Requires (Dev)
- friendsofphp/php-cs-fixer: ~2.0
- phpunit/phpunit: ~6.0
README
Yii2 ArrayQuery 组件
允许对数组进行搜索/筛选。当在 ArrayDataProvider 中显示数组数据时,此组件非常有用。
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一
php composer.phar require --prefer-dist yii2mod/yii2-array-query "*"
或者将以下内容添加到你的 composer.json 文件的 require 部分。
"yii2mod/yii2-array-query": "*"
to the require section of your composer.json.
查询数据
您可以使用 [[\yii2mod\query\ArrayQuery]] 类对数组数据进行复杂查询。此类与常规 [[\yii\db\Query]] 类似,使用相同的语法。例如
$data = [ [ 'id' => 1, 'username' => 'admin', 'email' => 'admin@example.com' ], [ 'id' => 2, 'username' => 'test', 'email' => 'test@example.com' ], ]; $query = new ArrayQuery(); $query->from($data); $query->where(['username' => 'admin']); $rows = $query->all();
与 ArrayDataProvider 一起使用
您可以使用 [[\yii2mod\query\ArrayQuery]] 类进行筛选。例如
<?php // Some search model /** * Setup search function for filtering and sorting. * @param $params * @return ArrayDataProvider */ public function search($params) { $models = User::find()->asArray()->all(); $query = new ArrayQuery(); $query->from($models); // load the search form data and validate if ($this->load($params) && $this->validate()) { // adjust the query by adding the filters $query->andFilterWhere(['id' => $this->id]); $query->andFilterWhere(['status' => $this->status]); $query->andFilterWhere(['like', 'username', $this->username]); $query->andFilterWhere(['like', 'email', $this->email]); } // prepare the ArrayDataProvider return new ArrayDataProvider([ 'allModels' => $query->indexBy('id')->all(), 'sort' => [ 'attributes' => ['id', 'username', 'email', 'status'], ], 'pagination' => [ 'pageSize' => 10 ], ]); }
支持我们
您的业务依赖于我们的贡献吗?请与我们联系,并在 Patreon 上支持我们。所有承诺都将用于分配人员维护和新奇的功能。