Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yii2mod/yii2-array-query
Yii2 component that allows for searching/filtering the elements of an array.
https://github.com/yii2mod/yii2-array-query
yii2 yii2-array-filter yii2-array-query yii2-array-search yii2-extension
Last synced: about 12 hours ago
JSON representation
Yii2 component that allows for searching/filtering the elements of an array.
- Host: GitHub
- URL: https://github.com/yii2mod/yii2-array-query
- Owner: yii2mod
- License: mit
- Created: 2016-08-29T15:09:48.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2019-11-02T17:40:11.000Z (almost 5 years ago)
- Last Synced: 2024-09-23T22:31:54.715Z (4 days ago)
- Topics: yii2, yii2-array-filter, yii2-array-query, yii2-array-search, yii2-extension
- Language: PHP
- Size: 20.5 KB
- Stars: 35
- Watchers: 8
- Forks: 11
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Yii2 ArrayQuery Component
Allows searching/filtering of an array. This component is very useful when displaying array data in GridViews with an
ArrayDataProvider.[![Latest Stable Version](https://poser.pugx.org/yii2mod/yii2-array-query/v/stable)](https://packagist.org/packages/yii2mod/yii2-array-query)
[![Total Downloads](https://poser.pugx.org/yii2mod/yii2-array-query/downloads)](https://packagist.org/packages/yii2mod/yii2-array-query)
[![License](https://poser.pugx.org/yii2mod/yii2-array-query/license)](https://packagist.org/packages/yii2mod/yii2-array-query)
[![Build Status](https://travis-ci.org/yii2mod/yii2-array-query.svg?branch=master)](https://travis-ci.org/yii2mod/yii2-array-query)Installation
------------The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
Either run
```
php composer.phar require --prefer-dist yii2mod/yii2-array-query "*"
```or add
```
"yii2mod/yii2-array-query": "*"
```to the require section of your composer.json.
Querying Data
-------------You may execute complex query on the array data using [[\yii2mod\query\ArrayQuery]] class. This class works similar to regular [[\yii\db\Query]] and uses same syntax. For example:
```php
$data = [
[
'id' => 1,
'username' => 'admin',
'email' => '[email protected]'
],
[
'id' => 2,
'username' => 'test',
'email' => '[email protected]'
],
];$query = new ArrayQuery();
$query->from($data);
$query->where(['username' => 'admin']);$rows = $query->all();
```Using with ArrayDataProvider
----------------------------You may perform filtering using [[\yii2mod\query\ArrayQuery]] class. For example:
```php
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
],
]);
}```
## Support us
Does your business depend on our contributions? Reach out and support us on [Patreon](https://www.patreon.com/yii2mod).
All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.